symbol vs string property lookup speed

Benchmark created by Nathan Ridley on


Setup

const bar = Symbol('bar');
  const baz = Symbol.for('baz');
  const value = {
    x: Math.random(),
    foo: function() { return 3 * this.x; },
    [bar]: function() { return 3 * this.x; },
    [baz]: function() { return 3 * this.x; }
  }
  
  function f(v) {
    return v.foo();
  }
  
  function g(v) {
    return v[bar]();
  }
  
  function h(v) {
    return v[baz]();
  }

Test runner

Ready to run.

Testing in
TestOps/sec
String Property
value.x = value.foo();
ready
Private Symbol Property
value.x = value[bar]();
ready
Global Symbol Property (via lookup)
value.x = value[Symbol.for('baz')]();
ready
Global Symbol Property (via reference)
value.x = value[baz]();
ready
Private Symbol Property (via function call)
value.x = g(value);
ready
Global Symbol Property (via reference and function call)
value.x = h(value);
ready
String Property (via function call)
value.x = f(value);
ready

Revisions

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

  • Revision 1: published by Nathan Ridley on