Skip to content

Instantly share code, notes, and snippets.

@viktorbezdek
Created January 23, 2023 14:23
Show Gist options
  • Save viktorbezdek/2052233f9501f26a66c7c9d98a42234a to your computer and use it in GitHub Desktop.
Save viktorbezdek/2052233f9501f26a66c7c9d98a42234a to your computer and use it in GitHub Desktop.
const _ = require("lodash/fp")
const sample = Array.from({ length: 90000 }).map((_, i) => i)
var nativeStart
var nativeDuration
;(() => {
nativeStart = process.hrtime.bigint()
for (let x = 0; x <= 10000; x++) {
sample.map((n) => n * 2)
}
nativeDuration = process.hrtime.bigint() - nativeStart
})()
var lodashStart
var lodashDuration
;(() => {
lodashStart = process.hrtime.bigint()
for (let y = 0; y <= 10000; y++) {
_.map((n) => n * 2, sample)
}
lodashDuration = process.hrtime.bigint() - lodashStart
})()
console.log(`
Node: ${process.version}
Native: ${Number(nativeDuration) / 1000000}ms
Lodash/fp: ${Number(lodashDuration) / 1000000}ms
Lodash/fp _.map can do the same thing ${(
(1 - 1 / (Number(nativeDuration) / Number(lodashDuration))) *
100
).toFixed(2)}% faster than native map
`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment