Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
Chrome 114
Windows
Desktop
one year ago
Test name Executions per second
isNaN 1.3 Ops/sec
regex 1.1 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)
    }
  }
});
const regex = /^\d+$/
window.case2 = new Proxy({}, {
  get(t, p, r) {
    if (regex.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]; }