Skip to content

Instantly share code, notes, and snippets.

@djmadeira
Last active March 16, 2017 14:33
Show Gist options
  • Save djmadeira/9d775e6ad96e40c54578868aca08c06e to your computer and use it in GitHub Desktop.
Save djmadeira/9d775e6ad96e40c54578868aca08c06e to your computer and use it in GitHub Desktop.
lodash.get vs destructuring wrapped in try catch vs unsafe destructuring (node 6.9.4)
'use strict';
const lodash = require('lodash');
function getObj() {
return {
prop1: {
0: { },
1: null,
2: {
key: 'value'
}
}
}
}
const start = process.hrtime();
for (let i = 0; i < 10000; i++) {
const num = 2;
try {
const {
prop1: {
[num]: {
key
}
}
} = getObj();
} catch(e) {
console.error(e);
}
}
const end = process.hrtime(start);
console.info("Execution time (hr): %ds %dms", end[0], end[1]/1000000);
console.log('test complete');
/*
Execution time (hr): 0s 4.17709ms
test complete
Execution time (hr): 0s 3.986169ms
test complete
Execution time (hr): 0s 3.844447ms
test complete
Execution time (hr): 0s 3.785207ms
test complete
Execution time (hr): 0s 3.964295ms
test complete
Execution time (hr): 0s 3.829094ms
test complete
*/
'use strict';
const lodash = require('lodash');
function getObj() {
return {
prop1: {
0: { },
1: null,
2: {
key: 'value'
}
}
}
}
const start = process.hrtime();
for (let i = 0; i < 10000; i++) {
const num = 2;
const {
prop1: {
[num]: {
key
}
}
} = getObj();
}
const end = process.hrtime(start);
console.info("Execution time (hr): %ds %dms", end[0], end[1]/1000000);
console.log('test complete');
/*
Execution time (hr): 0s 7.521536ms
test complete
Execution time (hr): 0s 6.068653ms
test complete
Execution time (hr): 0s 6.074837ms
test complete
Execution time (hr): 0s 6.061937ms
test complete
Execution time (hr): 0s 6.310838ms
test complete
Execution time (hr): 0s 6.040864ms
test complete
Execution time (hr): 0s 6.223818ms
test complete
Execution time (hr): 0s 6.35524ms
test complete
*/
'use strict';
const lodash = require('lodash');
function getObj() {
return {
prop1: {
0: { },
1: null,
2: {
key: 'value'
}
}
}
}
const start = process.hrtime();
for (let i = 0; i < 10000; i++) {
const num = 2;
const prop = lodash.get(getObj(), ['prop1', num, 'key']);
}
const end = process.hrtime(start);
console.info("Execution time (hr): %ds %dms", end[0], end[1]/1000000);
console.log('test complete');
/*
Execution time (hr): 0s 11.988349ms
test complete
Execution time (hr): 0s 14.576332ms
test complete
Execution time (hr): 0s 12.101122ms
test complete
Execution time (hr): 0s 14.44793ms
test complete
Execution time (hr): 0s 13.044243ms
test complete
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment