Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save billycromb/7d7ac6354b8e0093f38c652f3d80eeaf to your computer and use it in GitHub Desktop.

Select an option

Save billycromb/7d7ac6354b8e0093f38c652f3d80eeaf to your computer and use it in GitHub Desktop.
measure_filter_rig
'use strict';
(function() {
function filterArray(users) {
return Object.fromEntries(
users.filter(user => user.active)
.map(user => [user.name, null])
);
}
function randomName(length) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
let res = ''
for (let i = 0; i < length; i++) {
res = res + characters.charAt(Math.floor(Math.random() * charactersLength))
}
return res;
}
function randomBool() {
return (Math.floor(Math.random() * 2) == 0);
}
function setup() {
let users = [];
for (let i = 0; i < 10000000; i++) {
users[i] = {name: randomName(8), active: randomBool()}
}
return users;
}
function doBench() {
console.log('starting bench');
let a = setup();
console.log('done setup');
let then = new Date();
let b = filterArray(a);
let now = new Date();
console.log(now-then);
}
doBench();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment