Lookup Table, Ternary Operator, Switch and Brackets Property Syntax performance comparison

JavaScript performance comparison

Test case created by ozcanzaferayan

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const strings = {
      activity: {
          headerDates: {
              today: 'Bugün',
              tomorrow: 'Dün',
              thisWeek: 'Bu hafta',
              thisMonth: 'Bu ay',
              earlier: 'Daha öncekiler',
          }
      }
  };
  
  const longDates = [...Array(100)].map(x => ["today", "tomorrow", "thisWeek", "thisMonth", "earlier"][Math.floor(Math.random() * 5)])

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Lookup Table
longDates.map(x =>
        ({
            'today': y => strings.activity.headerDates.today,
            'tomorrow': y => strings.activity.headerDates.tomorrow,
            'thisWeek': y => strings.activity.headerDates.thisWeek,
            'thisMonth': y => strings.activity.headerDates.thisMonth,
            'earlier': y => strings.activity.headerDates.earlier
        })[x]());
pending…
Brackets Property Accessor
longDates.map(x => 
        strings.activity.headerDates[x] ||
        strings.activity.headerDates.earlier
    );
pending…
Nested Ternary Operator
longDates.map(x =>
        x === 'today' ? strings.activity.headerDates.today :
        x === 'tomorrow' ? strings.activity.headerDates.tomorrow :
        x === 'thisWeek' ? strings.activity.headerDates.thisWeek :
        x === 'thisMonth' ? strings.activity.headerDates.thisMonth :
        x === 'earlier' ? strings.activity.headerDates.earlier :
        'Daha Öncekiler');
pending…
Switch Case
longDates.map(x => {
        switch (x) {
            case 'today': return strings.activity.headerDates.today;
            case 'tomorrow': return strings.activity.headerDates.tomorrow;
            case 'thisWeek': return strings.activity.headerDates.thisWeek;
            case 'thisMonth': return strings.activity.headerDates.thisMonth;
            case 'earlier': return strings.activity.headerDates.earlier;
            default: return strings.activity.headerDates.earlier;
        }
    });
pending…

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments