Skip to content

Instantly share code, notes, and snippets.

@jasnell
Created May 24, 2021 21:58
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jasnell/9a28d9c2135fd055c5c8f1f067db4aff to your computer and use it in GitHub Desktop.
Save jasnell/9a28d9c2135fd055c5c8f1f067db4aff to your computer and use it in GitHub Desktop.
Getting loopy with JavaScript
'use strict';
const {
createHistogram,
performance: {
timerify
}
} = require('perf_hooks');
const users = [
{ active: true, id: 'a1', name: 'abc1' },
{ active: false, id: 'a2', name: 'abc2' },
{ active: true, id: 'a3', name: 'abc3' },
{ active: false, id: 'a4', name: 'abc4' },
{ active: false, id: 'a5', name: 'abc5' },
{ active: true, id: 'a6', name: 'abc6' },
{ active: false, id: 'a7', name: 'abc7' },
{ active: true, id: 'a8', name: 'abc8' },
{ active: false, id: 'a9', name: 'abc9' },
{ active: false, id: 'a10', name: 'abc10' },
{ active: true, id: 'a11', name: 'abc11' },
{ active: true, id: 'a12', name: 'abc12' },
{ active: false, id: 'a13', name: 'abc13' },
{ active: true, id: 'a14', name: 'abc14' },
{ active: true, id: 'a15', name: 'abc15' },
{ active: false, id: 'a16', name: 'abc16' },
{ active: true, id: 'a17', name: 'abc17' },
];
function option1(users) {
const result = {};
users.forEach((user) => {
if (!user.active)
result[user.id] = user.name;
});
return result;
}
function option2(users) {
return users.reduce((result, user) => {
if (!user.active)
result[user.id] = user.name;
return result;
}, {});
}
function option3(users) {
const result = {};
for (let user of users) {
if (!user.active)
result[user.id] = user.name;
}
return result;
}
function option4(users) {
const result = {};
for (let n = 0; n < users.length; n++) {
const user = users[n];
if (!user.active)
result[user.id] = user.name;
}
return result;
}
function option5(users) {
return users.filter((user) => !user.active)
.map((user) => ({ [user.id]: user.name }))
.reduce(Object.assign, {});
}
function option6(users) {
return users.reduce((acc, curr) => {
if (curr.active) return acc;
return {...acc, [curr.id]: curr.name };
}, {});
}
const h_option1 = createHistogram();
const t_option1 = timerify(option1, { histogram: h_option1 });
const h_option2 = createHistogram();
const t_option2 = timerify(option2, { histogram: h_option2 });
const h_option3 = createHistogram();
const t_option3 = timerify(option3, { histogram: h_option3 });
const h_option4 = createHistogram();
const t_option4 = timerify(option4, { histogram: h_option4 });
const h_option5 = createHistogram();
const t_option5 = timerify(option5, { histogram: h_option5 });
const h_option6 = createHistogram();
const t_option6 = timerify(option6, { histogram: h_option6 });
process.on('exit', () => {
console.log('option1', h_option1.min, h_option1.max, h_option1.mean, h_option1.stddev)
console.log('option2', h_option2.min, h_option2.max, h_option2.mean, h_option2.stddev)
console.log('option3', h_option3.min, h_option3.max, h_option3.mean, h_option3.stddev)
console.log('option4', h_option4.min, h_option4.max, h_option4.mean, h_option4.stddev)
console.log('option5', h_option5.min, h_option5.max, h_option5.mean, h_option5.stddev)
console.log('option6', h_option6.min, h_option6.max, h_option6.mean, h_option6.stddev)
});
for (let n = 0; n < 1e6; n++) {
t_option1(users);
t_option2(users);
t_option3(users);
t_option4(users);
t_option5(users);
t_option6(users);
}
@jasnell
Copy link
Author

jasnell commented May 24, 2021

option1 340 187647 410.657562 566.7615826961247
option2 330 173567 391.390447 545.9543098594792
option3 340 336639 405.218414 638.6245288018027
option4 330 182271 392.341791 533.6590191778945
option5 14328 926207 15716.732428 6277.805672464639
option6 2540 192511 2811.1838 1653.4001916491854

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment