Skip to content

Instantly share code, notes, and snippets.

@trentm trentm/ben-arrayiter.js
Last active Apr 12, 2016

Embed
What would you like to do?
Slow JavaScript array iter
var ben = require('ben');
var servers = [];
for (var i = 0; i < 1000; i++) {
servers.push({"uuid": "blah", "hostname": "blah", "vms": {"foo": "blah"}});
}
var forMs = ben(function () {
var filteredServers = [];
for (var i = 0; i < servers.length; i++) {
var server = servers[i];
//...
filteredServers.push(server);
}
});
var filterMs = ben(function () {
var filteredServers = servers.filter(function (server) {
//...
return true;
});
});
console.log('for-loop: %s milliseconds per iteration', forMs);
console.log('[].filter: %s milliseconds per iteration', filterMs);
$ node --version
v0.10.42
$ node ben-arrayiter.js
for-loop: 0.0077 milliseconds per iteration
[].filter: 0.0446 milliseconds per iteration
$ ~/opt/node-5/bin/node --version
v5.7.0
$ ~/opt/node-5/bin/node ben-arrayiter.js
for-loop: 0.0074 milliseconds per iteration
[].filter: 0.0212 milliseconds per iteration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.