Test name | Executions per second |
---|---|
_.get | 1579572.0 Ops/sec |
native | 563340.6 Ops/sec |
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var object = { 'a': [{ 'b': { 'c': 3 } }] };
_.get(object, 'a[0].b.c');
var object = { 'a': [{ 'b': { 'c': 3 } }] };
/*
const get = (obj, path, defaultValue) => {
const result = String.prototype.split.call(path, /[,[\].]+?/)
.filter(Boolean)
.reduce((res, key) => (res !== null && res !== undefined) ? res[key] : res, obj);
return (result === undefined || result === obj) ? defaultValue : result;
}
*/
const get = (obj, item, failure) => obj.item || failure;
get(object, 'a[0].b.c');