HTML Preparation code:
AخA
 
1
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
 
var obj = {a: {b: {c: {d: 1}}}}
var badObj = {}
Tests:
  • TS Optional Chaining result in javascript

     
    ((_c = (_b = (_a = obj === null || obj === void 0 ? void 0 : obj.a) === null || _a === void 0 ? void 0 : _a.b) === null || _b === void 0 ? void 0 : _b.c) === null || _c === void 0 ? void 0 : _c.d) || 2;
    ((_f = (_e = (_d = badObj === null || badObj === void 0 ? void 0 : badObj.a) === null || _d === void 0 ? void 0 : _d.b) === null || _e === void 0 ? void 0 : _e.c) === null || _f === void 0 ? void 0 : _f.d) || 2;
  • Lodash Get

     
    _.get(obj, "a.b.c.d", 2)
    _.get(badObj, "a.b.c.d", 2)
  • ES6 Optional Chaining

     
    obj?.a?.b?.c?.d || 2
    badObj?.a?.b?.c?.d || 2
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    TS Optional Chaining result in javascript
    Lodash Get
    ES6 Optional Chaining

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 2 years ago)
Mozilla/5.0 (Android 11; Mobile; rv:100.0) Gecko/100.0 Firefox/100.0
Firefox Mobile 100 on Android
View result in a separate tab
Test name Executions per second
TS Optional Chaining result in javascript 2615206.2 Ops/sec
Lodash Get 588321.5 Ops/sec
ES6 Optional Chaining 18488162.0 Ops/sec