Performance comparison of function inlining

by Arnaud Buchholz

JavaScript

function log (text) {
	var line = document.createElement("div");
  line.appendChild(document.createTextNode(text));
  document.body.appendChild(line);
}

function test (label, cb) {
	var dtStart = new Date(),
  		count = 0;
  while (new Date() - dtStart < 1000) {
  	cb();
    ++count;
  }
  log(label + ": " + count.toLocaleString('en-US'));
  return count;
}

function mul (a, b) {
	return a * b;
}

function add (a, b) {
	return a + b;
}


var a = test("Using function call", function () {

	result = mul(add(1, 2), 3);

});

var b = test("Inlining", function () {

	result = (1 + 2) * 3;

});

log("Gain/Loss: " + Math.floor(1000 * (b - a) / a)/10 + "%");