Last active
August 11, 2019 22:21
-
-
Save jcuffe/db1a1f47d29a23a887b4723d46ee2c19 to your computer and use it in GitHub Desktop.
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 { PerformanceObserver, performance } = require("perf_hooks") | |
// Parse useful information from performance events | |
const obs = new PerformanceObserver(items => { | |
console.log(items.getEntries().map(e => `${e.name} : ${e.duration}`)) | |
performance.clearMarks() | |
}) | |
obs.observe({ entryTypes: ["measure"] }) | |
// Big array for big work | |
const arr = new Array(5000).fill(0).map((_, i) => i + 1) | |
// Build lookup object using reduce, object literal and spread syntax | |
performance.mark("A") | |
const lookup = arr.reduce((o, v, i) => ({ [v]: i, ...o }), {}) | |
performance.mark("B") | |
performance.measure("Reduce lookup build", "A", "B") | |
// Build lookup object like an old fart with a for loop | |
performance.mark("C") | |
const lookup2 = {} | |
arr.forEach((v, i) => (lookup2[v] = i)) | |
performance.mark("D") | |
performance.measure("Sane lookup build", "C", "D") | |
// Build lookup object with reduce, but Object.assign instead of spread | |
performance.mark("E") | |
const lookup3 = arr.reduce((o, v, i) => Object.assign(o, { [v]: i }), {}) | |
performance.mark("F") | |
performance.measure("Compromise?", "E", "F") | |
// Ugliest reduce possible | |
performance.mark("G") | |
const lookup4 = arr.reduce((o, v, i) => { | |
o[v] = i | |
return o | |
}, {}) | |
performance.mark("H") | |
performance.measure("Yes", "G", "H") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment