Skip to content

Instantly share code, notes, and snippets.

@A
Created April 19, 2015 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save A/2017815ecf87caaffb56 to your computer and use it in GitHub Desktop.
Save A/2017815ecf87caaffb56 to your computer and use it in GitHub Desktop.
var noop = function () { return function (text) { return text + text; }; }
var mws = [noop(), noop(), noop()];
function forloop(text) {
var l = mws.length;
for (var i = 0; i < l; i++) {
text = mws[i].call(text, text) || text;
}
return text;
}
function reduce(text) {
return mws.reduce(function(text, mw) {
return mw.call(text, text) || text;
}, text);
}
function bench(fn) {
var startedAt = +new Date();
var endedAt = startedAt + 1000;
var i = 0;
do {
fn();
i++;
} while (+new Date() < endedAt);
console.log('%d cycles per second', i);
};
console.log('for loop');
bench(function () {
forloop('hello world!11..');
});
console.log('reduce');
bench(function() {
reduce('hello world!11..');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment