Skip to content

Instantly share code, notes, and snippets.

@Hypercubed
Last active August 29, 2015 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hypercubed/c1c7bf3ccea167ecb56b to your computer and use it in GitHub Desktop.
Save Hypercubed/c1c7bf3ccea167ecb56b to your computer and use it in GitHub Desktop.
PowerArray vs....
node_modules/
var PowerArray = require('PowerArray'),
fast = require('fast.js'),
_ = require('underscore')
lodash = require('lodash');
var i,
LEN = 1e7,
array = Array();
function format(s) {
return (s[0] * 1e3 + s[1] / 1e6) / 1e3;
}
function ops_per_sec(s) {
return (LEN / format(s)).toExponential();
}
function report(s,t) {
console.log(s+' complete in ' + format(t) + ', ops/s: ' + ops_per_sec(t));
}
function bench(s, fn, n) {
var t = process.hrtime();
fn();
t = process.hrtime(t);
var ops = ops_per_sec(t);
n = n || ops;
s += '\t' + format(t) + ' s\t' + ops + ' ops/s';
s += '\t( '+Math.round((ops-n)/n*100, 2)+'% )'
console.log(s);
return ops;
}
function xbench(s) {
//console.log(s+' (pending)');
};
function rand() {
return Math.floor(Math.random() * 100 + 1);
}
for (i=0; i < LEN; i++) {
var rnd = rand();
array.push(rnd);
var p = i/LEN*100;
if (p % 10 === 0) {
console.log(p+'% complete');
}
}
var parray = new PowerArray(array);
var farray = new Float32Array(array);
var n = bench('array.forEach', function() {
array.forEach(function (i) {
i * 2;
});
});
bench('Array.prototype.forEach on array', function() {
Array.prototype.forEach.call(array,function (i) {
i * 2;
});
}, n);
bench('Array.prototype.forEach on Float32Array', function() {
Array.prototype.forEach.call(farray,function (i) {
i * 2;
});
}, n);
bench('Array.prototype.forEach on PowerArray', function() {
Array.prototype.forEach.call(parray, function (i) {
i * 2;
});
}, n);
bench('powerArray.forEach', function() {
parray.forEach(function (i) {
i * 2;
});
}, n);
bench('fast.js on Array', function() {
fast.forEach(array,function (i) {
i * 2;
});
}, n);
bench('fast.js on Float32Array', function() {
fast.forEach(farray,function (i) {
i * 2;
});
}, n);
bench('fast.js on PowerArray', function() {
fast.forEach(parray,function (i) {
i * 2;
});
}, n);
bench('underscore on Array', function() {
_.forEach(array,function (i) {
i * 2;
});
}, n);
bench('underscore on Float32Array', function() {
_.forEach(farray,function (i) {
i * 2;
});
}, n);
bench('lodash on Array', function() {
lodash.forEach(array,function (i) {
i * 2;
});
}, n);
bench('lodash on Float32Array', function() {
lodash.forEach(farray,function (i) {
i * 2;
});
}, n);
| | time (s) | ops/s | %pct chg |
| array.forEach | 0.695366052 | 1.44E+07 |
| Array.prototype.forEach on array | 0.740159474 | 1.35E+07 | -6.05% |
| Array.prototype.forEach on Float32Array | 1.368684138 | 7.31E+06 | -49.19% |
| Array.prototype.forEach on PowerArray | 0.751636273 | 1.33E+07 | -7.49% |
| powerArray.forEach | 0.175532694 | 5.70E+07 | 296.15% |
| fast.js on Array | 0.184384148 | 5.42E+07 | 277.13% |
| fast.js on Float32Array | 6.121523157 | 1.63E+06 | -88.64% |
| fast.js on PowerArray | 0.182967407 | 5.47E+07 | 280.05% |
| underscore on Array | 0.179662224 | 5.57E+07 | 287.04% |
| underscore on Float32Array | 0.246603782 | 4.06E+07 | 181.98% |
| lodash on Array | 0.182177956 | 5.49E+07 | 281.70% |
| lodash on Float32Array | 0.245795857 | 4.07E+07 | 182.90% |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment