Run details:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36
Chrome 109
Linux
Desktop
one year ago
Test name Executions per second
isNaN 1.5 Ops/sec
regex 1.3 Ops/sec
Script Preparation code:
AخA
 
window.case1 = new Proxy({}, {
  get(t, p, r) {
    if (typeof p === 'string' && !isNaN(p)) {
      return +p
    }
    else {
      return Reflect.get(t, p, r)
    }
  }
});
window.case2 = new Proxy({}, {
  get(t, p, r) {
    if (/^\d+$/.test(p)) {
      return +p
    }
    else {
      return Reflect.get(t, p, r)
    }
  }
});
window.gabage1 = 0;
window.gabage2 = 0;
Tests:
  • isNaN

     
    for (let i = 0; i < 1e6;i++) { gabage1 =+ case1[i]; }
  • regex

     
    for (let i = 0; i < 1e6;i++) { gabage2 += case2[i]; }