HTML Preparation code:
AخA
 
1
<script src="https://cdn.jsdelivr.net/lodash/4.16.0/lodash.min.js"></script>
Script Preparation code:
 
var person = {name: 'Frederick', lastName: 'Corcino Alejo', nested: {name: 'test'}};
function safeGet(obj, path, defaultValue) {
  return path.split('.').reduce((xs, x) => (xs && xs[x]) ? xs[x] : defaultValue, obj);
}
Tests:
  • Lodash get

     
    _.get(person, 'name', '');
    _.get(person, 'nested.name', '');
  • safeGet

     
    safeGet(person, 'name', '');
    safeGet(person, 'nested.name', '');
  • Null operator

     
    person?.name ?? '';
    person?.nested?.name ?? '';
  • Or Operator

     
    person?.name || '';
    person?.nested?.name || '';
  • Direct

     
    person.name;
    person.nested.name;
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Lodash get
    safeGet
    Null operator
    Or Operator
    Direct

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 6 months ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Chrome 130 on Windows
View result in a separate tab
Test name Executions per second
Lodash get 3904029.2 Ops/sec
safeGet 5681454.0 Ops/sec
Null operator 17298478.0 Ops/sec
Or Operator 17397892.0 Ops/sec
Direct 17467270.0 Ops/sec