native vs. array.js vs. underscore vs lo-dash

JavaScript performance comparison

Revision 8 of this test case created by Mark Johnson and last updated

Info

This test compares the performance of a native array vs. array.js vs. underscore vs lo-dash

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script>
<script>
var underscore = _.noConflict();
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<script>
var lodash = _.noConflict();
</script>
<script>
(function(){function g(h,f,k){var c=g.resolve(h);if(null==c)throw k=k||h,f=f||"root",c=Error('Failed to require "'+k+'" from "'+f+'"'),c.path=k,c.parent=f,c.require=!0,c;f=g.modules[c];f.exports||(f.exports={},f.client=f.component=!0,f.call(this,f.exports,g.relative(c),f));return f.exports}var l=Object.prototype.hasOwnProperty;g.modules={};g.aliases={};g.resolve=function(h){"/"===h.charAt(0)&&(h=h.slice(1));for(var f=h+"/index.js",k=[h,h+".js",h+".json",h+"/index.js",h+"/index.json"],c=0;c<k.length;c++)if(h=
k[c],l.call(g.modules,h))return h;if(l.call(g.aliases,f))return g.aliases[f]};g.normalize=function(h,f){var g=[];if("."!=f.charAt(0))return f;h=h.split("/");f=f.split("/");for(var c=0;c<f.length;++c)".."==f[c]?h.pop():"."!=f[c]&&""!=f[c]&&g.push(f[c]);return h.concat(g).join("/")};g.register=function(h,f){g.modules[h]=f};g.alias=function(h,f){if(!l.call(g.modules,h))throw Error('Failed to alias "'+h+'", it does not exist');g.aliases[f]=h};g.relative=function(h){function f(c){var d=f.resolve(c);return g(d,
h,c)}var k=g.normalize(h,"..");f.resolve=function(c){var d=c.charAt(0);if("/"==d)return c.slice(1);if("."==d)return g.normalize(k,c);var d=h.split("/"),a;a:{for(a=d.length;a--;)if("deps"===d[a])break a;a=-1}(a+=1)||(a=0);return c=d.slice(0,a+1).join("/")+"/deps/"+c};f.exists=function(c){return l.call(g.modules,f.resolve(c))};return f};g.register("component-emitter/index.js",function(h,f,g){function c(d){if(d){for(var a in c.prototype)d[a]=c.prototype[a];return d}}g.exports=c;c.prototype.on=function(c,
a){this._callbacks=this._callbacks||{};(this._callbacks[c]=this._callbacks[c]||[]).push(a);return this};c.prototype.once=function(c,a){function j(){b.off(c,j);a.apply(this,arguments)}var b=this;this._callbacks=this._callbacks||{};a._off=j;this.on(c,j);return this};c.prototype.off=function(c,a){this._callbacks=this._callbacks||{};var j=this._callbacks[c];if(!j)return this;if(1==arguments.length)return delete this._callbacks[c],this;var b=j.indexOf(a._off||a);~b&&j.splice(b,1);return this};c.prototype.emit=
function(c){this._callbacks=this._callbacks||{};var a=[].slice.call(arguments,1),j=this._callbacks[c];if(j)for(var j=j.slice(0),b=0,e=j.length;b<e;++b)j[b].apply(this,a);return this};c.prototype.listeners=function(c){this._callbacks=this._callbacks||{};return this._callbacks[c]||[]};c.prototype.hasListeners=function(c){return!!this.listeners(c).length}});g.register("component-to-function/index.js",function(h,f,g){function c(a){switch({}.toString.call(a)){case "[object Object]":var j={},b;for(b in a)j[b]=
"string"===typeof a[b]?d(a[b]):c(a[b]);return function(a){if("object"!==typeof a)return!1;for(var b in j)if(!(b in a)||!j[b](a[b]))return!1;return!0};case "[object Function]":return a;case "[object String]":return b=/^ *\W+/.test(a)?new Function("_","return _ "+a):new Function("_","return _."+a),b;case "[object RegExp]":return function(b){return a.test(b)};default:return d(a)}}function d(a){return function(c){return a===c}}g.exports=c});g.register("array/index.js",function(g,f,k){k.exports=f("./lib/array")});
g.register("array/lib/array.js",function(g,f,k){function c(a){if(!(this instanceof c))return new c(a);a=a||[];for(var j=this.length=a.length,b=0;b<j;b++)this[b]=a[b]}g=f("emitter");f=f("./enumerable");var d=Array.prototype;k.exports=c;g(c.prototype);f(c.prototype);c.prototype.pop=function(){var a=d.pop.apply(this,arguments);this.emit("remove",a);return a};c.prototype.push=function(){for(var a=d.push.apply(this,arguments),c=[].slice.call(arguments),b=0,e=c.length;b<e;b++)this.emit("add",c[b]);return a};
c.prototype.shift=function(){var a=d.shift.apply(this,arguments);this.emit("remove",a);return a};c.prototype.splice=function(){for(var a=d.splice.apply(this,arguments),c=[].slice.call(arguments,2),b=0,e=a.length;b<e;b++)this.emit("remove",a[b]);b=0;for(e=c.length;b<e;b++)this.emit("add",c[b]);return a};c.prototype.unshift=function(){for(var a=d.unshift.apply(this,arguments),c=[].slice.call(arguments),b=0,e=c.length;b<e;b++)this.emit("add",c[b]);return a};c.prototype.toString=c.prototype.toJSON=function(){return d.slice.call(this)};
"sort reverse concat join slice lastIndexOf".split(" ").forEach(function(a){c.prototype[a]=function(){return d[a].apply(this,arguments)}})});g.register("array/lib/enumerable.js",function(g,f,k){var c=f("to-function"),d={};k.exports=function(a){for(var c in d)a[c]=d[c];return a};d.forEach=d.each=function(a){for(var c=this.length,b=0;b<c;b++)a(this[b],b);return this};d.map=function(a){a=c(a);for(var j=this.length,b=0;b<j;++b)this[b]=a(this[b],b);return this};d.filter=d.select=function(a){a=c(a);for(var j=
[],b=this.length,e,d=0;d<b;++d)e=this[d],a(e,d)&&j.push(e);return new this.constructor(j)};d.unique=function(){for(var a=[],c=this.length,b,e=0;e<c;++e)b=this[e],~this.indexOf(b)||a.push(b);return new this.constructor(a)};d.reject=function(a){var j=[],b=this.length,e,d;"string"==typeof a&&(a=c(a));if(a)for(d=0;d<b;++d)e=this[d],a(this[d],d)||j.push(e);else for(d=0;d<b;++d)e=this[d],e!=a&&j.push(e);return new this.constructor(j)};d.compact=function(){return this.reject(null)};d.find=function(a){a=
c(a);for(var j=this.length,b,e=0;e<j;++e)if(b=this[e],a(b,e))return b};d.findLast=function(a){a=c(a);for(var j=this.length,b,e,d=0;d<j;++d)e=this[d],a(e,d)&&(b=e);return b};d.every=function(a){a=c(a);for(var d=this.length,b,e=0;e<d;++e)if(b=this[e],!a(b,e))return!1;return!0};d.none=function(a){a=c(a);for(var d=this.length,b,e=0;e<d;++e)if(b=this[e],a(b,e))return!1;return!0};d.any=function(a){a=c(a);for(var d=this.length,b,e=0;e<d;++e)if(b=this[e],a(b,e))return!0;return!1};d.count=function(a){a=c(a);
var d=0,b=this.length,e;if(!a)return b;for(var f=0;f<b;++f)e=this[f],a(e,f)&&++d;return d};d.indexOf=function(a){for(var c=this.length,b,e=0;e<c;++e)if(b=this[e],b===a)return e;return-1};d.has=function(a){return!!~this.indexOf(a)};d.reduce=function(a,c){var b=this.length,e=0,d;for(d=null==c?this.get(e++):c;e<b;++e)d=a(d,this[e],e);return d};d.max=function(a){var d=this.length,b=-Infinity,e=0,f;if(a){a=c(a);for(f=0;f<d;++f)e=a(this[f],f),b=e>b?e:b}else for(f=0;f<d;++f)e=this[f],b=e>b?e:b;return b};
d.min=function(a){var d=this.length,b=Infinity,e=0,f;if(a){a=c(a);for(f=0;f<d;++f)e=a(this[f],f),b=e<b?e:b}else for(f=0;f<d;++f)e=this[f],b=e<b?e:b;return b};d.sum=function(a){var d=this.length,b=0,e;if(a){a=c(a);for(e=0;e<d;++e)b+=a(this[e],e)}else for(e=0;e<d;++e)b+=this[e];return b};d.avg=d.mean=function(a){var d=this.length,b=0,e;if(a){a=c(a);for(e=0;e<d;++e)b+=a(this[e],e)}else for(e=0;e<d;++e)b+=this[e];return b/d};d.first=function(a){if(a){if("number"!==typeof a)return this.find(a)}else return this[0];
a=Math.min(a,this.length);for(var c=Array(a),b=0;b<a;++b)c[b]=c[b];return c};d.last=function(a){var c=this.length;if(a){if("number"!==typeof a)return this.findLast(a)}else return this[c-1];a=Math.max(0,c-a);for(var b=[];a<c;++a)b.push(this[a]);return b}});g.alias("component-emitter/index.js","array/deps/emitter/index.js");g.alias("component-to-function/index.js","array/deps/to-function/index.js");"object"==typeof exports?module.exports=g("array"):"function"==typeof define&&define.amd?define(g("array")):
window.array=g("array")})();
</script>
<script>
Benchmark.prototype.setup = function() {
    var arr = underscore.shuffle(underscore.range(50));
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
native
var r = arr.map(function(num) {
  return num + num;
});
pending…
array.js
var r = arr.map(function(num) {
  return num * num;
});
pending…
underscore
var r = underscore.map(arr, function(num) {
  return num * num;
});
pending…
lo-dash
var r = lodash.map(arr, function(num) {
  return num * num;
});
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment