Created
May 4, 2018 06:28
-
-
Save maxvipon/0ae7fc21e3afa5fb0b8a4e47c7ca56ba to your computer and use it in GitHub Desktop.
Bench Arr.some vs Set.has
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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