Created
May 24, 2021 21:58
-
-
Save jasnell/9a28d9c2135fd055c5c8f1f067db4aff to your computer and use it in GitHub Desktop.
Getting loopy with JavaScript
This file contains 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
'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); | |
} |
Author
jasnell
commented
May 24, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment