Last active
April 1, 2022 08:56
-
-
Save technikhil314/584ac968bfd756d091c377650cd1daa1 to your computer and use it in GitHub Desktop.
Benchmarking of lodash vs _.get
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
#! /usr/bin/env node | |
const _ = require('lodash'); | |
const { | |
performance, | |
} = require('perf_hooks'); | |
const isValidInput = process.argv[2] !== "" && !isNaN(Number(process.argv[2])); | |
const size = isValidInput ? Number(process.argv[2]) : 100000; | |
const arr = new Array(size).fill({ | |
key: { | |
nestedKey: { | |
deepNestedKey: { | |
veryDeepNestedKey: { | |
shitloadDeepNestedKey: { | |
val: 10 | |
} | |
} | |
} | |
} | |
} | |
}) | |
function optionalChainingBenchmarking(arr) { | |
const t0 = performance.now(); | |
for (let i = 0; i < size; i++) { | |
const val = arr[i]?.key?.nestedKey?.deepNestedKey?.veryDeepNestedKey?.shitloadDeepNestedKey?.val | |
} | |
const t1 = performance.now(); | |
console.log(`Call to read nested keys for ${size} objects using optional chaining took ${t1 - t0} milliseconds.`); | |
} | |
function underscoreGetBenchmarking(arr) { | |
const t0 = performance.now(); | |
for (let i = 0; i < size; i++) { | |
const val = _.get(arr[i], "key.nestedKey.deepNestedKey.veryDeepNestedKey.shitloadDeepNestedKey.val") | |
} | |
const t1 = performance.now(); | |
console.log(`Call to read nested keys for ${size} using _.get took ${t1 - t0} milliseconds.`); | |
} | |
optionalChainingBenchmarking(arr); | |
underscoreGetBenchmarking(arr); |
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
{ | |
"name": "optionalChaniningBenchmarkiing", | |
"version": "1.0.0", | |
"description": "", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"engines": { | |
"node": ">=14" | |
}, | |
"bin": "./index.js", | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"lodash": "^4.17.21" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment