Array Performance

JavaScript performance comparison

Revision 191 of this test case created by Mo

Preparation code

<script src="https://raw.github.com/lodash/lodash/3.7.0/lodash.min.js"></script>
<script>
  Benchmark.prototype.setup = function() {
  	var shuff = _.shuffle(_.sample(_.range(10000),100);
  	var target =  _.sample(shuff);

  	var a1 = _.map(shuff, function(id) {
  		return {
  			id: id,
  			name: 'name'+id
  		};
  	});

  	var a2 = _.reduce(shuff, function(r, id) {
  		r[id] = {
  			id: id,
  			name: 'name'+id
  		};
  		return r;
  	}, []);

  	var o = _.reduce(shuff, function(r, id) {
  		r[id] = {
  			id: id,
  			name: 'name'+id
  		};
  		return r;
  	}, {});
  };

</script>
      
<script>
Benchmark.prototype.setup = function() {
  target =  _.sample(shuff);

};
</script>

Preparation code output

<script> Benchmark.prototype.setup = function() { var shuff = _.shuffle(_.sample(_.range(10000),100); var target = _.sample(shuff); var a1 = _.map(shuff, function(id) { return { id: id, name: 'name'+id }; }); var a2 = _.reduce(shuff, function(r, id) { r[id] = { id: id, name: 'name'+id }; return r; }, []); var o = _.reduce(shuff, function(r, id) { r[id] = { id: id, name: 'name'+id }; return r; }, {}); }; </script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Manual Array Lookup
for (var i = 0; i < a1.length; i++) {
  if (a1[i].id = target) result = a1[i];
}
pending…
Holey Array by Index
var id = 29938;
var result = a2[target];
pending…
Object by Key
target
var result = o[id];
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.

0 Comments