Skip to content

Instantly share code, notes, and snippets.

@maxvipon
Created May 4, 2018 06:28
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 maxvipon/0ae7fc21e3afa5fb0b8a4e47c7ca56ba to your computer and use it in GitHub Desktop.
Save maxvipon/0ae7fc21e3afa5fb0b8a4e47c7ca56ba to your computer and use it in GitHub Desktop.
Bench Arr.some vs Set.has
const Benchmark = require('benchmark').Benchmark;
const suite = new Benchmark.Suite;
const users = new Array(50).fill().map(gen);
const logins = new Array(10).fill().map(gen);
function random(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function gen() {
let text = '';
let possible = 'abcdefghijklmnopqrstuvwxyz0123456789-';
let len = random(2, 13)
for (let i = 0; i < len; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
suite
.add('some', function() {
let x = logins.reduce((accesses, login) => {
accesses[login] = users.some(user => user.login === login);
return accesses;
}, {});
})
.add('Set ', function() {
let users_set = new Set(users);
let x = logins.reduce((accesses, login) => {
accesses[login] = users_set.has(login);
return accesses;
}, {});
})
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run({async: true});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment