Skip to content

Instantly share code, notes, and snippets.

@technikhil314
Last active April 1, 2022 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save technikhil314/584ac968bfd756d091c377650cd1daa1 to your computer and use it in GitHub Desktop.
Save technikhil314/584ac968bfd756d091c377650cd1daa1 to your computer and use it in GitHub Desktop.
Benchmarking of lodash vs _.get
#! /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);
{
"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