Run details:
Mozilla/5.0 (Linux; Android 10; Surface Duo) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.105 Mobile Safari/537.36
Chrome Mobile 89
Android
Mobile
3 years ago
Test name Executions per second
isNaN 0.7 Ops/sec
regex 0.7 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]; }