Faster way to remove arbitrary items in an array

node v0.12.18
version: 1.0.0
endpointsharetweet
const Benchmark = require("benchmark"); let suite = new Benchmark.Suite; // add tests suite.add('Removal method', function() { let list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; let toRemoveIx = [0, 3, 5]; toRemoveIx.forEach(function(ix) { list.splice(ix, 1); }); }) .add('Keeping method', function() { let list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; let toKeep = [1, 3, 5]; list.splice.apply(list, [0, list.length].concat(toKeep)); }) .on('cycle', function(event) { console.log(String(event.target)); }) .on('complete', function() { console.log('Fastest is ' + this.filter('fastest').map('name')); }) .run({ 'async': true });
Loading…

no comments

    sign in to comment