lambdas as getters

Benchmark created on


Description

What's the cost of a simple () => myObj.prop; getter?

Setup

var myObj = { a: 1 , b: 2, c: 3 };
// Need to combat lambda inlining.
var getters = [
  () => myObj.a,
  () => myObj.b,
  () => myObj.c
];
var gettersWithComputation = [
  () => myObj.a + 6,
  () => myObj.b + 6,
  () => myObj.c + 6
];

var myIndirectObj = {
	a: { value: 1 }, 
	b: { value: 2 },
	c: { value: 3 }
};
var indirects = [
    myIndirectObj.a,
    myIndirectObj.b,
    myIndirectObj.c
]


var oneThousandGetters = [];
var oneThousandGettersWithComputation = [];
var oneThousandIndirects = [];

for (var i = 0; i < 1000; i++) {
	var index = Math.floor(Math.random(index));
	oneThousandGetters.push(getters[index]);
	oneThousandGettersWithComputation.push(gettersWithComputation[index]);
	oneThousandIndirects.push(indirects[index]);
}

Test runner

Ready to run.

Testing in
TestOps/sec
Direct access
for (var i = 0; i < 1000; i++) {
	var g1 = oneThousandGetters[i];
	var g2 = oneThousandGettersWithComputation[i];
	var ind = oneThousandIndirects[i];
	myObj.a + 6
}
ready
Simple getter
for (var i = 0; i < 1000; i++) {
	var g1 = oneThousandGetters[i];
	var g2 = oneThousandGettersWithComputation[i];
	var ind = oneThousandIndirects[i];
	g1() + 6
}
ready
Getter with included computation
for (var i = 0; i < 1000; i++) {
	var g1 = oneThousandGetters[i];
	var g2 = oneThousandGettersWithComputation[i];
	var ind = oneThousandIndirects[i];
	g2()
}
ready
Readonly indirect object as getter
for (var i = 0; i < 1000; i++) {
	var g1 = oneThousandGetters[i];
	var g2 = oneThousandGettersWithComputation[i];
	var ind = oneThousandIndirects[i];
	ind.value + 6
}
ready

Revisions

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