Skip to content

Instantly share code, notes, and snippets.

@jcmoore
Created December 7, 2013 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcmoore/7837935 to your computer and use it in GitHub Desktop.
Save jcmoore/7837935 to your computer and use it in GitHub Desktop.
var Laser = function (method, dependencies) {
this.__ = method;
this.$$ = dependencies;
};
var z = 1;
var add = function (x, y) {
return x + y + this.$$.z;
};
add.__ = new Laser(add, {z:z}); // add_this
var f = function () {
var add_this = this.$$.add.__;
var sum = 0;
for (var i = 0; i < 10000; i++) {
// invoke add() with add_this as "this" -- thus exposing the z value
sum = add_this.__(sum, i);
}
//if (sum != 49995000) throw "argh";
};
f.__ = new Laser(f, {add:add}); // f_this
function measure(f) {
var start = new Date().valueOf();
var f_this = f.__;
for (var i = 0; i < 100000; i++) {
// invoke f() with f_this as "this" -- thus exposing the add() method
f_this.__();
}
var end = new Date().valueOf();
print(end - start);
}
measure(f); // 9.24s+
// (no inlining)
var z = 1;
var add = function (x, y) {
return x + y + this.$$.z;
};
add.__ = {}; // add_this
add.__.__ = add; // add_this.__ === add
add.__.$$ = { // add_this.$$ -> add()'s dependencies
z: z,
};
var f = function () {
var add_this = this.$$.add.__;
var sum = 0;
for (var i = 0; i < 10000; i++) {
// invoke add() with add_this as "this" -- thus exposing the z value
sum = add_this.__(sum, i);
}
//if (sum != 49995000) throw "argh";
};
f.__ = {}; // f_this
f.__.__ = f; // f_this.__ === f
f.__.$$ = { // f_this.$$ -> f()'s dependencies
add: add,
};
function measure(f) {
var start = new Date().valueOf();
var f_this = f.__;
for (var i = 0; i < 100000; i++) {
// invoke f() with f_this as "this" -- thus exposing the add() method
f_this.__();
}
var end = new Date().valueOf();
print(end - start);
}
measure(f); // 1.65s+
// Inlined add called from f.
var z = 1;
var add = function (x, y) {
return x + y + this.$$.z;
};
add.__ = { // add_this
__: add, // add_this.__ === add
$$: { // add_this.$$ -> add()'s dependencies
z: z,
},
};
var f = function () {
var add_this = this.$$.add.__;
var sum = 0;
for (var i = 0; i < 10000; i++) {
// invoke add() with add_this as "this" -- thus exposing the z value
sum = add_this.__(sum, i);
}
//if (sum != 49995000) throw "argh";
};
f.__ = { // f_this
__: f, // f_this.__ === f
$$: { // f_this.$$ -> f()'s dependencies
add: add,
},
};
function measure(f) {
var start = new Date().valueOf();
var f_this = f.__;
for (var i = 0; i < 100000; i++) {
// invoke f() with f_this as "this" -- thus exposing the add() method
f_this.__();
}
var end = new Date().valueOf();
print(end - start);
}
measure(f); // 9.24s+
// (no inlining)
var helper = function (method, dependencies) {
var __ = {};
__.__ = method;
__.$$ = dependencies;
return __;
};
var z = 1;
var add = function (x, y) {
return x + y + this.$$.z;
};
add.__ = helper(add, {z:z}); // add_this
var f = function () {
var add_this = this.$$.add.__;
var sum = 0;
for (var i = 0; i < 10000; i++) {
// invoke add() with add_this as "this" -- thus exposing the z value
sum = add_this.__(sum, i);
}
//if (sum != 49995000) throw "argh";
};
f.__ = helper(f, {add:add}); // f_this
function measure(f) {
var start = new Date().valueOf();
var f_this = f.__;
for (var i = 0; i < 100000; i++) {
// invoke f() with f_this as "this" -- thus exposing the add() method
f_this.__();
}
var end = new Date().valueOf();
print(end - start);
}
measure(f); // 1.65s+
// Inlined add called from f.
var z = 1;
var add = (function (w) {
return function add (x, y) {
return x + y + w;
};
}) (z);
var f = (function (plus) {
return function f () {
var sum = 0;
for (var i = 0; i < 10000; i++) {
sum = plus(sum, i);
}
//if (sum != 49995000) throw "argh";
};
}) (add);
function measure(f) {
var start = new Date().valueOf();
for (var i = 0; i < 100000; i++) {
// invoke f() with f_this as "this" -- thus exposing the add() method
f();
}
var end = new Date().valueOf();
print(end - start);
}
measure(f); // 2.22s+
// Inlined add called from f. Inlined add called from f. Inlined f called from measure.
// (I do not know why "add" was inlined in "f" twice...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment