Destructuring Assignment

Benchmark created on


Description

Object properties vs Array index

Setup

var size = 16384;
var typed = new Int32Array(size);
crypto.getRandomValues(typed);
var acc = 0;
var retrieveProp = (x) => ({
	a: typed[x],
	b: typed[x+1],
 	c: typed[x+2],
	d: typed[x+3],
	});
var retrieveArray = (x) => ([
	typed[x],
	typed[x+1],
 	typed[x+2],
	typed[x+3],
]);
var argBuff = new Int32Array(4);
var retrieveTyped = (x, arr) => {
	arr[0] = typed[x];
	arr[1] = typed[x+1];
	arr[2] = typed[x+2];
	arr[3] = typed[x+3];
};

Test runner

Ready to run.

Testing in
TestOps/sec
object property
for(let x = 0; x < 16384; x+=4) {
	let {a,b,c,d} = retrieveProp(x);
	acc += a + b + c + d;
}
ready
array index
for(let x = 0; x < 16384; x+=4) {
	let [a,b,c,d] = retrieveArray(x);
	acc += a + b + c + d;
}
ready
typed arg buffer
for(let x = 0; x < 16384; x+=4) {
	retrieveTyped(x, argBuff);
	acc += argBuff[0]
	     + argBuff[1]
	     + argBuff[2]
	     + argBuff[3];
}
ready

Revisions

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