peach.js

A precompiled forEach, unrolled for faster runtime performance.

Stars
4

April 24th 2012

Project discontinued in favour of https://github.com/bestiejs/lodash which has implemented the same technique of function compilation and applied it to underscore.js. Thanks goes to @jdalton for his earlier help [1] [2] with this project.


pEach(iterator:Function, [timesToUnroll:Number]):Function takes functions you would pass to an iterator such as _.each() or Array.forEach and returns another which is optimised for runtime performance.

How?

  1. Loop unwinding is applied.
  2. The contents of your function are extracted and combined with the unrolled loop.
  3. The loop construct used is the fastest loop for the browser the user is using.

Example

Take an iterator that you would pass to something like _.each

_.each(['foo','bar','baz'], function (num, key, collection) {
  console.log(num, key, collection, this);
});

Pass it to pEach

var optimised = pEach(function (num, key, collection) {
  console.log(num, key, collection, this);
});

and use the optimised function instead

// Array
optimised(['foo','bar','baz']);

// Object
optimised({
  foo: 'foo',
  bar: 'bar',
  baz: 'baz'
});