Prop traversal vs Locally cached variable

Edit on GitHub

Property traversal vs caching in local variables.

Setup

const obj = {
  x: {a: 1, b: 2, c: 3},
  y: {a: 1, b: { m: 11, n: 12, o: 13}, c: 3},
  z: 20
};
const concat = (a, b) => a + b

Tests

  • Prop traversal

    concat(obj.x.a, obj.y.b.m);
    concat(obj.z, obj.y.b.m);
    concat(obj.x.a, obj.z);
  • Locally cached variable

    const xa = obj.x.a;
    const ybm = obj.y.b.m;
    let z = obj.z;
    
    concat(xa, ybm);
    concat(z, ybm);
    concat(xa, z);