codemix/fast.js — pull request #21 by phpnode (2014-06-26 21:08:32+00:00)
Title: Experimental macros

This is an experiment to see whether sweet.js macros can provide a significant speed compared to the library functions. Only a few have been implemented so far, and only the map has benchmarks, but so far the answer is - "it depends"

```
Running 3 benchmarks, please wait...

Native .map() vs fast.map() (3 items)
✓ Array::map() x 2,162,291 ops/sec ±2.01% (83 runs sampled)
✓ fast.map() x 13,036,174 ops/sec ±1.64% (84 runs sampled)
✓ fast.map() v0.0.0 x 10,670,088 ops/sec ±1.43% (91 runs sampled)
✓ fast-map (macro) x 20,796,506 ops/sec ±1.74% (87 runs sampled)
✓ fast-map (macro, inline) x 4,612,038 ops/sec ±1.70% (80 runs sampled)
✓ fast-map (macro, expression) x 10,084,483 ops/sec ±1.88% (88 runs sampled)
✓ fast-map (macro, inline expression) x 11,086,628 ops/sec ±1.50% (90 runs sampled)
✓ underscore.map() x 1,848,111 ops/sec ±1.88% (80 runs sampled)
✓ lodash.map() x 8,294,632 ops/sec ±1.61% (89 runs sampled)

Winner is: fast-map (macro) (1025.28% faster)

Native .map() vs fast.map() (10 items)
✓ Array::map() x 1,259,921 ops/sec ±1.83% (90 runs sampled)
✓ fast.map() x 5,236,904 ops/sec ±2.00% (83 runs sampled)
✓ fast.map() v0.0.0 x 4,053,922 ops/sec ±1.64% (85 runs sampled)
✓ fast-map (macro) x 10,830,563 ops/sec ±1.78% (83 runs sampled)
✓ fast-map (macro, inline) x 3,213,971 ops/sec ±2.00% (77 runs sampled)
✓ fast-map (macro, expression) x 3,896,702 ops/sec ±1.75% (91 runs sampled)
✓ fast-map (macro, inline expression) x 3,960,513 ops/sec ±1.79% (91 runs sampled)
✓ underscore.map() x 1,191,063 ops/sec ±1.64% (89 runs sampled)
✓ lodash.map() x 3,895,571 ops/sec ±1.86% (90 runs sampled)

Winner is: fast-map (macro) (809.32% faster)

Native .map() vs fast.map() (1000 items)
✓ Array::map() x 26,864 ops/sec ±1.52% (91 runs sampled)
✓ fast.map() x 65,885 ops/sec ±1.58% (91 runs sampled)
✓ fast.map() v0.0.0 x 48,661 ops/sec ±1.64% (93 runs sampled)
✓ fast-map (macro) x 137,222 ops/sec ±1.66% (89 runs sampled)
✓ fast-map (macro, inline) x 77,559 ops/sec ±1.81% (88 runs sampled)
✓ fast-map (macro, expression) x 47,945 ops/sec ±1.97% (87 runs sampled)
✓ fast-map (macro, inline expression) x 47,138 ops/sec ±1.95% (88 runs sampled)
✓ underscore.map() x 26,391 ops/sec ±1.76% (93 runs sampled)
✓ lodash.map() x 56,014 ops/sec ±1.63% (84 runs sampled)

Winner is: fast-map (macro) (419.96% faster)
```

These results indicate that the fastest pattern is this:

```js
var items = [1,2,3,4,5];

function doSomething (item) {
return item + item;
}

function go () {
fast-map items as result doSomething;
return result;
}

console.log(go());
```
i.e. the iterator should be defined in a separate function, not as a child of the function doing the work.

(Syntax is also experimental and open to discussion)