Skip to content

Instantly share code, notes, and snippets.

@jcmoore
Created December 3, 2013 17:25
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/7773475 to your computer and use it in GitHub Desktop.
Save jcmoore/7773475 to your computer and use it in GitHub Desktop.
pew.js prototype -- alternative approach to javascript closures aiming for higher v8 performance
var Functor = (function () {}).constructor;
var CHARGE = function () {
var mode;
var dependencies;
var method;
var vars = arguments.length;
method = (vars-- > 0) ? arguments[vars] : null;
dependencies = (vars-- > 0) ? arguments[vars] : null;
mode = (vars-- > 0) ? arguments[vars] : null;
var text = method.toString();
var argStart = text.indexOf("(") + 1;
var argEnd = text.indexOf(")");
var argDiff = argEnd - argStart;
var bodyStart = text.indexOf("{") + 1;
var bodyEnd = text.lastIndexOf("}");
var bodyDiff = bodyEnd - bodyStart;
var pew = Functor (text.substr(argStart, argDiff), '"use strict";\n' + text.substr(bodyStart, bodyDiff));
var count = dependencies ? dependencies.length : 0;
var index;
var d;
var symbol = 0;
var value = 1;
var meta = 2;
var deps = {};
deps.__ = pew;
if (count > 0) {
// where possible, order dependencies by most popular/frequently depended upon is first
for (index = 0; index < count; index++) {
d = dependencies[index];
deps[d[symbol]] = d[value];
}
}
pew.__ = deps;
return pew;
};
// a Laser can be used to pass additional information to a CHARGEd method on "this"
// pew is the method (use .__.__ to access the deps/dependencies)
// variants should be the extra data to attache (potentially "array-wrapped" to preserve monomorphic-ness)
var Laser = function (pew, variants) {
this.__ = pew;
this.$$ = variants;
};
(function () {
var z = 1;
function add(x, y) {
return x + y;
}
function f() {
var sum = 0;
for (var i = 0; i < 10000; i++) {
sum = add(sum, i);
}
if (sum != 49995000) throw "argh";
}
function measure(f) {
var start = new Date().valueOf();
for (var i = 0; i < 100000; i++) {
f();
}
var end = new Date().valueOf();
print(end - start);
}
measure(f); // 1.31s+
}) ();
var Functor = (function () {}).constructor;
var CHARGE = function () {
var mode;
var dependencies;
var method;
var vars = arguments.length;
method = (vars-- > 0) ? arguments[vars] : null;
dependencies = (vars-- > 0) ? arguments[vars] : null;
mode = (vars-- > 0) ? arguments[vars] : null;
var text = method.toString();
var argStart = text.indexOf("(") + 1;
var argEnd = text.indexOf(")");
var argDiff = argEnd - argStart;
var bodyStart = text.indexOf("{") + 1;
var bodyEnd = text.lastIndexOf("}");
var bodyDiff = bodyEnd - bodyStart;
var pew = Functor (text.substr(argStart, argDiff), '"use strict";\n' + text.substr(bodyStart, bodyDiff));
var count = dependencies ? dependencies.length : 0;
var index;
var d;
var symbol = 0;
var value = 1;
var meta = 2;
var deps = {};
deps.__ = pew;
if (count > 0) {
// where possible, order dependencies by most popular/frequently depended upon is first
for (index = 0; index < count; index++) {
d = dependencies[index];
deps[d[symbol]] = d[value];
}
}
pew.__ = deps;
return pew;
};
// a Laser can be used to pass additional information to a CHARGEd method on "this"
// pew is the method (use .__.__ to access the deps/dependencies)
// variants should be the extra data to attache (potentially "array-wrapped" to preserve monomorphic-ness)
var Laser = function (pew, variants) {
this.__ = pew;
this.$$ = variants;
};
(function () {
var z = 1;
var add = function (x, y) {
return x + y;
}
var f = function () {
var sum = 0;
for (var i = 0; i < 10000; i++) {
sum = add(sum, i);
}
//if (sum != 49995000) throw "argh";
}
function measure(f) {
var start = new Date().valueOf();
for (var i = 0; i < 100000; i++) {
f();
}
var end = new Date().valueOf();
print(end - start);
}
measure(f); // 1.89s+
}) ();
var Functor = (function () {}).constructor;
var CHARGE = function () {
var mode;
var dependencies;
var method;
var vars = arguments.length;
method = (vars-- > 0) ? arguments[vars] : null;
dependencies = (vars-- > 0) ? arguments[vars] : null;
mode = (vars-- > 0) ? arguments[vars] : null;
var text = method.toString();
var argStart = text.indexOf("(") + 1;
var argEnd = text.indexOf(")");
var argDiff = argEnd - argStart;
var bodyStart = text.indexOf("{") + 1;
var bodyEnd = text.lastIndexOf("}");
var bodyDiff = bodyEnd - bodyStart;
var pew = Functor (text.substr(argStart, argDiff), '"use strict";\n' + text.substr(bodyStart, bodyDiff));
var count = dependencies ? dependencies.length : 0;
var index;
var d;
var symbol = 0;
var value = 1;
var meta = 2;
var deps = {};
deps.__ = pew;
if (count > 0) {
// where possible, order dependencies by most popular/frequently depended upon is first
for (index = 0; index < count; index++) {
d = dependencies[index];
deps[d[symbol]] = d[value];
}
}
pew.__ = deps;
return pew;
};
// a Laser can be used to pass additional information to a CHARGEd method on "this"
// pew is the method (use .__.__ to access the deps/dependencies)
// variants should be the extra data to attache (potentially "array-wrapped" to preserve monomorphic-ness)
var Laser = function (pew, variants) {
this.__ = pew;
this.$$ = variants;
};
(function () {
var z = 1;
var add = function (x, y) {
return x + y;
}
var f = CHARGE(null, [["add", add]], function () {
var sum = 0;
for (var i = 0; i < 10000; i++) {
sum = this.add(sum, i);
}
//if (sum != 49995000) throw "argh";
});
function measure(f) {
var start = new Date().valueOf();
for (var i = 0; i < 100000; i++) {
f.__.__();
}
var end = new Date().valueOf();
print(end - start);
}
measure(f); // 1.9s +
}) ();
var Functor = (function () {}).constructor;
var CHARGE = function () {
var mode;
var dependencies;
var method;
var vars = arguments.length;
method = (vars-- > 0) ? arguments[vars] : null;
dependencies = (vars-- > 0) ? arguments[vars] : null;
mode = (vars-- > 0) ? arguments[vars] : null;
var text = method.toString();
var argStart = text.indexOf("(") + 1;
var argEnd = text.indexOf(")");
var argDiff = argEnd - argStart;
var bodyStart = text.indexOf("{") + 1;
var bodyEnd = text.lastIndexOf("}");
var bodyDiff = bodyEnd - bodyStart;
var pew = Functor (text.substr(argStart, argDiff), '"use strict";\n' + text.substr(bodyStart, bodyDiff));
var count = dependencies ? dependencies.length : 0;
var index;
var d;
var symbol = 0;
var value = 1;
var meta = 2;
var deps = {};
deps.__ = pew;
if (count > 0) {
// where possible, order dependencies by most popular/frequently depended upon is first
for (index = 0; index < count; index++) {
d = dependencies[index];
deps[d[symbol]] = d[value];
}
}
pew.__ = deps;
return pew;
};
// a Laser can be used to pass additional information to a CHARGEd method on "this"
// pew is the method (use .__.__ to access the deps/dependencies)
// variants should be the extra data to attache (potentially "array-wrapped" to preserve monomorphic-ness)
var Laser = function (pew, variants) {
this.__ = pew;
this.$$ = variants;
};
(function () {
var z = 1;
var add = function (x, y) {
return x + y;
}
var f = CHARGE(null, [["add", add]], function () {
var sum = 0;
for (var i = 0; i < 10000; i++) {
sum = this.add(sum, i);
}
//if (sum != 49995000) throw "argh";
});
function measure(f) {
var start = new Date().valueOf();
var pew = f.__;
for (var i = 0; i < 100000; i++) {
pew.__();
}
var end = new Date().valueOf();
print(end - start);
}
measure(f); // 1.85s +
}) ();
var Functor = (function () {}).constructor;
var CHARGE = function () {
var mode;
var dependencies;
var method;
var vars = arguments.length;
method = (vars-- > 0) ? arguments[vars] : null;
dependencies = (vars-- > 0) ? arguments[vars] : null;
mode = (vars-- > 0) ? arguments[vars] : null;
var text = method.toString();
var argStart = text.indexOf("(") + 1;
var argEnd = text.indexOf(")");
var argDiff = argEnd - argStart;
var bodyStart = text.indexOf("{") + 1;
var bodyEnd = text.lastIndexOf("}");
var bodyDiff = bodyEnd - bodyStart;
var pew = Functor (text.substr(argStart, argDiff), '"use strict";\n' + text.substr(bodyStart, bodyDiff));
var count = dependencies ? dependencies.length : 0;
var index;
var d;
var symbol = 0;
var value = 1;
var meta = 2;
var deps = {};
deps.__ = pew;
if (count > 0) {
// where possible, order dependencies by most popular/frequently depended upon is first
for (index = 0; index < count; index++) {
d = dependencies[index];
deps[d[symbol]] = d[value];
}
}
pew.__ = deps;
return pew;
};
// a Laser can be used to pass additional information to a CHARGEd method on "this"
// pew is the method (use .__.__ to access the deps/dependencies)
// variants should be the extra data to attache (potentially "array-wrapped" to preserve monomorphic-ness)
var Laser = function (pew, variants) {
this.__ = pew;
this.$$ = variants;
};
(function () {
var z = 1;
function add (x, y) {
return x + y + z;
}
function f () {
var sum = 0;
for (var i = 0; i < 10000; i++) {
sum = add(sum, i);
}
//if (sum != 49995000) throw "argh";
}
function measure(f) {
var start = new Date().valueOf();
for (var i = 0; i < 100000; i++) {
f();
}
var end = new Date().valueOf();
print(end - start);
}
measure(f); // 2.19s+
}) ();
var Functor = (function () {}).constructor;
var CHARGE = function () {
var mode;
var dependencies;
var method;
var vars = arguments.length;
method = (vars-- > 0) ? arguments[vars] : null;
dependencies = (vars-- > 0) ? arguments[vars] : null;
mode = (vars-- > 0) ? arguments[vars] : null;
var text = method.toString();
var argStart = text.indexOf("(") + 1;
var argEnd = text.indexOf(")");
var argDiff = argEnd - argStart;
var bodyStart = text.indexOf("{") + 1;
var bodyEnd = text.lastIndexOf("}");
var bodyDiff = bodyEnd - bodyStart;
var pew = Functor (text.substr(argStart, argDiff), '"use strict";\n' + text.substr(bodyStart, bodyDiff));
var count = dependencies ? dependencies.length : 0;
var index;
var d;
var symbol = 0;
var value = 1;
var meta = 2;
var deps = {};
deps.__ = pew;
if (count > 0) {
// where possible, order dependencies by most popular/frequently depended upon is first
for (index = 0; index < count; index++) {
d = dependencies[index];
deps[d[symbol]] = d[value];
}
}
pew.__ = deps;
return pew;
};
// a Laser can be used to pass additional information to a CHARGEd method on "this"
// pew is the method (use .__.__ to access the deps/dependencies)
// variants should be the extra data to attache (potentially "array-wrapped" to preserve monomorphic-ness)
var Laser = function (pew, variants) {
this.__ = pew;
this.$$ = variants;
};
(function () {
var z = 1;
var add = function (x, y) {
return x + y + z;
}
var f = function () {
var sum = 0;
for (var i = 0; i < 10000; i++) {
sum = add(sum, i);
}
//if (sum != 49995000) throw "argh";
}
function measure(f) {
var start = new Date().valueOf();
for (var i = 0; i < 100000; i++) {
f();
}
var end = new Date().valueOf();
print(end - start);
}
measure(f); // 2.19s+
}) ();
var Functor = (function () {}).constructor;
var CHARGE = function () {
var mode;
var dependencies;
var method;
var vars = arguments.length;
method = (vars-- > 0) ? arguments[vars] : null;
dependencies = (vars-- > 0) ? arguments[vars] : null;
mode = (vars-- > 0) ? arguments[vars] : null;
var text = method.toString();
var argStart = text.indexOf("(") + 1;
var argEnd = text.indexOf(")");
var argDiff = argEnd - argStart;
var bodyStart = text.indexOf("{") + 1;
var bodyEnd = text.lastIndexOf("}");
var bodyDiff = bodyEnd - bodyStart;
var pew = Functor (text.substr(argStart, argDiff), '"use strict";\n' + text.substr(bodyStart, bodyDiff));
var count = dependencies ? dependencies.length : 0;
var index;
var d;
var symbol = 0;
var value = 1;
var meta = 2;
var deps = {};
deps.__ = pew;
if (count > 0) {
// where possible, order dependencies by most popular/frequently depended upon is first
for (index = 0; index < count; index++) {
d = dependencies[index];
deps[d[symbol]] = d[value];
}
}
pew.__ = deps;
return pew;
};
// a Laser can be used to pass additional information to a CHARGEd method on "this"
// pew is the method (use .__.__ to access the deps/dependencies)
// variants should be the extra data to attache (potentially "array-wrapped" to preserve monomorphic-ness)
var Laser = function (pew, variants) {
this.__ = pew;
this.$$ = variants;
};
(function () {
var z = 1;
var add = function (x, y) {
return x + y + z;
}
var f = CHARGE(null, [["add", add]], function () {
var sum = 0;
for (var i = 0; i < 10000; i++) {
sum = this.add(sum, i);
}
//if (sum != 49995000) throw "argh";
});
function measure(f) {
var start = new Date().valueOf();
for (var i = 0; i < 100000; i++) {
f.__.__();
}
var end = new Date().valueOf();
print(end - start);
}
measure(f); // 2.19s+
}) ();
var Functor = (function () {}).constructor;
var CHARGE = function () {
var mode;
var dependencies;
var method;
var vars = arguments.length;
method = (vars-- > 0) ? arguments[vars] : null;
dependencies = (vars-- > 0) ? arguments[vars] : null;
mode = (vars-- > 0) ? arguments[vars] : null;
var text = method.toString();
var argStart = text.indexOf("(") + 1;
var argEnd = text.indexOf(")");
var argDiff = argEnd - argStart;
var bodyStart = text.indexOf("{") + 1;
var bodyEnd = text.lastIndexOf("}");
var bodyDiff = bodyEnd - bodyStart;
var pew = Functor (text.substr(argStart, argDiff), '"use strict";\n' + text.substr(bodyStart, bodyDiff));
var count = dependencies ? dependencies.length : 0;
var index;
var d;
var symbol = 0;
var value = 1;
var meta = 2;
var deps = {};
deps.__ = pew;
if (count > 0) {
// where possible, order dependencies by most popular/frequently depended upon is first
for (index = 0; index < count; index++) {
d = dependencies[index];
deps[d[symbol]] = d[value];
}
}
pew.__ = deps;
return pew;
};
// a Laser can be used to pass additional information to a CHARGEd method on "this"
// pew is the method (use .__.__ to access the deps/dependencies)
// variants should be the extra data to attache (potentially "array-wrapped" to preserve monomorphic-ness)
var Laser = function (pew, variants) {
this.__ = pew;
this.$$ = variants;
};
(function () {
var z = 1;
var add = CHARGE(null, [["z", z]], function (x, y) {
return x + y + this.z;
});
var f = CHARGE(null, [["add", add]], function () {
var sum = 0;
for (var i = 0; i < 10000; i++) {
sum = this.add.__.__(sum, i);
}
//if (sum != 49995000) throw "argh";
});
function measure(f) {
var start = new Date().valueOf();
for (var i = 0; i < 100000; i++) {
f.__.__();
}
var end = new Date().valueOf();
print(end - start);
}
measure(f); // 1.65s+
}) ();
var Functor = (function () {}).constructor;
var CHARGE = function () {
var mode;
var dependencies;
var method;
var vars = arguments.length;
method = (vars-- > 0) ? arguments[vars] : null;
dependencies = (vars-- > 0) ? arguments[vars] : null;
mode = (vars-- > 0) ? arguments[vars] : null;
var text = method.toString();
var argStart = text.indexOf("(") + 1;
var argEnd = text.indexOf(")");
var argDiff = argEnd - argStart;
var bodyStart = text.indexOf("{") + 1;
var bodyEnd = text.lastIndexOf("}");
var bodyDiff = bodyEnd - bodyStart;
var pew = Functor (text.substr(argStart, argDiff), '"use strict";\n' + text.substr(bodyStart, bodyDiff));
var count = dependencies ? dependencies.length : 0;
var index;
var d;
var symbol = 0;
var value = 1;
var meta = 2;
var deps = {};
deps.__ = pew;
if (count > 0) {
// where possible, order dependencies by most popular/frequently depended upon is first
for (index = 0; index < count; index++) {
d = dependencies[index];
deps[d[symbol]] = d[value];
}
}
pew.__ = deps;
return pew;
};
// a Laser can be used to pass additional information to a CHARGEd method on "this"
// pew is the method (use .__.__ to access the deps/dependencies)
// variants should be the extra data to attache (potentially "array-wrapped" to preserve monomorphic-ness)
var Laser = function (pew, variants) {
this.__ = pew;
this.$$ = variants;
};
(function () {
var z = 1;
var add = CHARGE(null, [["z", z]], function (x, y) {
return x + y + this.z;
});
var f = CHARGE(null, [["add", add]], function () {
var sum = 0;
for (var i = 0; i < 10000; i++) {
sum = this.add.__.__(sum, i);
}
//if (sum != 49995000) throw "argh";
});
function measure(f) {
var start = new Date().valueOf();
var pew = f.__;
for (var i = 0; i < 100000; i++) {
pew.__();
}
var end = new Date().valueOf();
print(end - start);
}
measure(f); // 1.65s+
}) ();
var Functor = (function () {}).constructor;
var CHARGE = function () {
var mode;
var dependencies;
var method;
var vars = arguments.length;
method = (vars-- > 0) ? arguments[vars] : null;
dependencies = (vars-- > 0) ? arguments[vars] : null;
mode = (vars-- > 0) ? arguments[vars] : null;
var text = method.toString();
var argStart = text.indexOf("(") + 1;
var argEnd = text.indexOf(")");
var argDiff = argEnd - argStart;
var bodyStart = text.indexOf("{") + 1;
var bodyEnd = text.lastIndexOf("}");
var bodyDiff = bodyEnd - bodyStart;
var pew = Functor (text.substr(argStart, argDiff), '"use strict";\n' + text.substr(bodyStart, bodyDiff));
var count = dependencies ? dependencies.length : 0;
var index;
var d;
var symbol = 0;
var value = 1;
var meta = 2;
var deps = {};
deps.__ = pew;
if (count > 0) {
// where possible, order dependencies by most popular/frequently depended upon is first
for (index = 0; index < count; index++) {
d = dependencies[index];
deps[d[symbol]] = d[value];
}
}
pew.__ = deps;
return pew;
};
// a Laser can be used to pass additional information to a CHARGEd method on "this"
// pew is the method (use .__.__ to access the deps/dependencies)
// variants should be the extra data to attache (potentially "array-wrapped" to preserve monomorphic-ness)
var Laser = function (pew, variants) {
this.__ = pew;
this.$$ = variants;
};
(function () {
var z = 1;
var add = CHARGE(null, [["z", z]], function (x, y) {
return x + y + this.z;
});
var f = CHARGE(null, [["add", add]], function () {
var sum = 0;
var pew = this.add.__;
for (var i = 0; i < 10000; i++) {
sum = pew.__(sum, i);
}
//if (sum != 49995000) throw "argh";
});
function measure(f) {
var start = new Date().valueOf();
var pew = f.__;
for (var i = 0; i < 100000; i++) {
pew.__();
}
var end = new Date().valueOf();
print(end - start);
}
measure(f); // 1.65s+
}) ();
var Functor = (function () {}).constructor;
var CHARGE = function () {
var mode;
var dependencies;
var method;
var vars = arguments.length;
method = (vars-- > 0) ? arguments[vars] : null;
dependencies = (vars-- > 0) ? arguments[vars] : null;
mode = (vars-- > 0) ? arguments[vars] : null;
var text = method.toString();
var argStart = text.indexOf("(") + 1;
var argEnd = text.indexOf(")");
var argDiff = argEnd - argStart;
var bodyStart = text.indexOf("{") + 1;
var bodyEnd = text.lastIndexOf("}");
var bodyDiff = bodyEnd - bodyStart;
var pew = Functor (text.substr(argStart, argDiff), '"use strict";\n' + text.substr(bodyStart, bodyDiff));
var count = dependencies ? dependencies.length : 0;
var index;
var d;
var symbol = 0;
var value = 1;
var meta = 2;
var deps = {};
deps.__ = pew;
if (count > 0) {
// where possible, order dependencies by most popular/frequently depended upon is first
for (index = 0; index < count; index++) {
d = dependencies[index];
deps[d[symbol]] = d[value];
}
}
pew.__ = deps;
return pew;
};
// a Laser can be used to pass additional information to a CHARGEd method on "this"
// pew is the method (use .__.__ to access the deps/dependencies)
// variants should be the extra data to attache (potentially "array-wrapped" to preserve monomorphic-ness)
var Laser = function (pew, variants) {
this.__ = pew;
this.$$ = variants;
};
(function () {
var z = 1;
function add (x, y) {
z = -z;
return x + y + z;
}
function f () {
var sum = 0;
for (var i = 0; i < 10000; i++) {
sum = add(sum, i);
}
//if (sum != 49995000) throw "argh";
}
function measure(f) {
var start = new Date().valueOf();
for (var i = 0; i < 100000; i++) {
f();
}
var end = new Date().valueOf();
print(end - start);
}
measure(f); // 4.42s+
}) ();
var Functor = (function () {}).constructor;
var CHARGE = function () {
var mode;
var dependencies;
var method;
var vars = arguments.length;
method = (vars-- > 0) ? arguments[vars] : null;
dependencies = (vars-- > 0) ? arguments[vars] : null;
mode = (vars-- > 0) ? arguments[vars] : null;
var text = method.toString();
var argStart = text.indexOf("(") + 1;
var argEnd = text.indexOf(")");
var argDiff = argEnd - argStart;
var bodyStart = text.indexOf("{") + 1;
var bodyEnd = text.lastIndexOf("}");
var bodyDiff = bodyEnd - bodyStart;
var pew = Functor (text.substr(argStart, argDiff), '"use strict";\n' + text.substr(bodyStart, bodyDiff));
var count = dependencies ? dependencies.length : 0;
var index;
var d;
var symbol = 0;
var value = 1;
var meta = 2;
var deps = {};
deps.__ = pew;
if (count > 0) {
// where possible, order dependencies by most popular/frequently depended upon is first
for (index = 0; index < count; index++) {
d = dependencies[index];
deps[d[symbol]] = d[value];
}
}
pew.__ = deps;
return pew;
};
// a Laser can be used to pass additional information to a CHARGEd method on "this"
// pew is the method (use .__.__ to access the deps/dependencies)
// variants should be the extra data to attache (potentially "array-wrapped" to preserve monomorphic-ness)
var Laser = function (pew, variants) {
this.__ = pew;
this.$$ = variants;
};
(function () {
var z = 1;
var add = function (x, y) {
z = -z;
return x + y + z;
}
var f = function () {
var sum = 0;
for (var i = 0; i < 10000; i++) {
sum = add(sum, i);
}
//if (sum != 49995000) throw "argh";
}
function measure(f) {
var start = new Date().valueOf();
for (var i = 0; i < 100000; i++) {
f();
}
var end = new Date().valueOf();
print(end - start);
}
measure(f); // 4.43s+
}) ();
var Functor = (function () {}).constructor;
var CHARGE = function () {
var mode;
var dependencies;
var method;
var vars = arguments.length;
method = (vars-- > 0) ? arguments[vars] : null;
dependencies = (vars-- > 0) ? arguments[vars] : null;
mode = (vars-- > 0) ? arguments[vars] : null;
var text = method.toString();
var argStart = text.indexOf("(") + 1;
var argEnd = text.indexOf(")");
var argDiff = argEnd - argStart;
var bodyStart = text.indexOf("{") + 1;
var bodyEnd = text.lastIndexOf("}");
var bodyDiff = bodyEnd - bodyStart;
var pew = Functor (text.substr(argStart, argDiff), '"use strict";\n' + text.substr(bodyStart, bodyDiff));
var count = dependencies ? dependencies.length : 0;
var index;
var d;
var symbol = 0;
var value = 1;
var meta = 2;
var deps = {};
deps.__ = pew;
if (count > 0) {
// where possible, order dependencies by most popular/frequently depended upon is first
for (index = 0; index < count; index++) {
d = dependencies[index];
deps[d[symbol]] = d[value];
}
}
pew.__ = deps;
return pew;
};
// a Laser can be used to pass additional information to a CHARGEd method on "this"
// pew is the method (use .__.__ to access the deps/dependencies)
// variants should be the extra data to attache (potentially "array-wrapped" to preserve monomorphic-ness)
var Laser = function (pew, variants) {
this.__ = pew;
this.$$ = variants;
};
(function () {
var z = 1;
var add = CHARGE(null, [["w", {z:z}]], function (x, y) {
this.w.z = -this.w.z;
return x + y + this.w.z;
});
var f = CHARGE(null, [["add", add]], function () {
var sum = 0;
var pew = this.add.__;
for (var i = 0; i < 10000; i++) {
sum = pew.__(sum, i);
}
//if (sum != 49995000) throw "argh";
});
function measure(f) {
var start = new Date().valueOf();
var pew = f.__;
for (var i = 0; i < 100000; i++) {
pew.__();
}
var end = new Date().valueOf();
print(end - start);
}
measure(f); // 3.68s+
}) ();
var Functor = (function () {}).constructor;
var CHARGE = function () {
var mode;
var dependencies;
var method;
var vars = arguments.length;
method = (vars-- > 0) ? arguments[vars] : null;
dependencies = (vars-- > 0) ? arguments[vars] : null;
mode = (vars-- > 0) ? arguments[vars] : null;
var text = method.toString();
var argStart = text.indexOf("(") + 1;
var argEnd = text.indexOf(")");
var argDiff = argEnd - argStart;
var bodyStart = text.indexOf("{") + 1;
var bodyEnd = text.lastIndexOf("}");
var bodyDiff = bodyEnd - bodyStart;
var pew = Functor (text.substr(argStart, argDiff), '"use strict";\n' + text.substr(bodyStart, bodyDiff));
var count = dependencies ? dependencies.length : 0;
var index;
var d;
var symbol = 0;
var value = 1;
var meta = 2;
var deps = {};
deps.__ = pew;
if (count > 0) {
// where possible, order dependencies by most popular/frequently depended upon is first
for (index = 0; index < count; index++) {
d = dependencies[index];
deps[d[symbol]] = d[value];
}
}
pew.__ = deps;
return pew;
};
// a Laser can be used to pass additional information to a CHARGEd method on "this"
// pew is the method (use .__.__ to access the deps/dependencies)
// variants should be the extra data to attache (potentially "array-wrapped" to preserve monomorphic-ness)
var Laser = function (pew, variants) {
this.__ = pew;
this.$$ = variants;
};
(function () {
var z = 1;
var add = CHARGE(null, [["z", [z]]], function (x, y) {
this.z[0] = -this.z[0];
return x + y + this.z[0];
});
var f = CHARGE(null, [["add", add]], function () {
var sum = 0;
var pew = this.add.__;
for (var i = 0; i < 10000; i++) {
sum = pew.__(sum, i);
}
//if (sum != 49995000) throw "argh";
});
function measure(f) {
var start = new Date().valueOf();
var pew = f.__;
for (var i = 0; i < 100000; i++) {
pew.__();
}
var end = new Date().valueOf();
print(end - start);
}
measure(f); // 3.65s+
}) ();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment