Created
December 5, 2013 18:16
-
-
Save jcmoore/7810459 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Functor = (function () {}).constructor; | |
var debug = function (mode) { | |
return false; | |
}; | |
debug.__ = { | |
__: debug, | |
$$: { | |
}, | |
}; | |
var dupe = function (method, mode) { | |
var __ = this.__.__.$$; | |
if (__.debug.__.__(mode)) { // use a proxy | |
return __.Functor ("method", "return function () { return method.apply(this, arguments); };") (method); | |
} | |
var text = __.toSource.call(method); | |
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; | |
return __.Functor (text.substr(argStart, argDiff), '"use strict";\n' + text.substr(bodyStart, bodyDiff)); | |
}; | |
dupe.__ = { | |
__: dupe, | |
$$: { | |
debug: debug, | |
toSource: Functor.prototype.toString, | |
Functor: Functor, | |
}, | |
}; | |
var ECHO = function (method, charge) { | |
var __ = this.__.__.$$; | |
charge = charge || method.__; | |
var pewpew = __.dupe.__.__(method); | |
pewpew.__ = { | |
__: pewpew, | |
$$: charge.$$, | |
}; | |
return pewpew; | |
}; | |
ECHO.__ = { | |
__: ECHO, | |
$$: { | |
dupe: dupe, | |
}, | |
}; | |
var CHARGE = function () { | |
var __ = this.__.__.$$; | |
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 pewpew = method; | |
if (!__.debug.__.__(mode)) { | |
pewpew = __.dupe.__.__(method); | |
} | |
var count = dependencies ? dependencies.length : 0; | |
var index; | |
var d; | |
var symbol = 0; | |
var value = 1; | |
var meta = 2; | |
var deps = null; | |
if (count > 0) { | |
deps = {}; | |
// 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]; | |
} | |
} | |
pewpew.__ = {}; // 3.63s | |
pewpew.__.__ = pewpew; | |
pewpew.__.$$ = deps; | |
//pewpew.__ = { // 16.62s | |
// __: pewpew, | |
// $$: deps, | |
//}; | |
//pewpew.__ = { // 16.81s | |
// $$: deps, | |
// __: pewpew, | |
//}; | |
//pewpew.__ = pewpew.$$(deps); // 3.62s | |
//pewpew.__ = new Laser(pewpew, deps); // 16.87s | |
return pewpew; | |
}; | |
CHARGE.__ = { | |
__: CHARGE, | |
$$: { | |
debug: debug, | |
dupe: dupe, | |
}, | |
}; | |
// 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) | |
Functor.prototype.Laser = function (pewpew, variants) { | |
var laser = {}; | |
laser.__ = pewpew; | |
laser.$$ = variants; | |
return laser; | |
}; | |
Functor.prototype.$$ = function (variants) { | |
return this.Laser(this, variants); | |
}; | |
var Laser = function (pewpew, variants) { | |
this.__ = pewpew; | |
this.$$ = variants; | |
}; | |
//(function () { | |
var z = {$$:1}; | |
var add = CHARGE.__.__(null, [["z", z]], function (x, y) { | |
var w = this.__.__.$$.z; | |
w.$$ = -w.$$; | |
return x + y + w.$$; | |
}); | |
var f = CHARGE.__.__(null, [["add", add]], function () { | |
var plus = this.__.__.$$.add; | |
var sum = 0; | |
for (var i = 0; i < 10000; i++) { | |
sum = plus.__.__(sum, i); | |
} | |
//if (sum != 49995000) throw "argh"; | |
}); | |
function measure(f) { | |
var start = new Date().valueOf(); | |
var l = f.$$({}); | |
l = { | |
__: f, | |
$$: f.__.$$, | |
}; | |
for (var i = 0; i < 100000; i++) { | |
f.__.__(); | |
} | |
var end = new Date().valueOf(); | |
print(end - start); | |
} | |
measure(f); // times listed on lines 119 - 135 | |
//}) (); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Functor = (function () {}).constructor; | |
// reducing closure/context reads (by grouping dependencies into one object) | |
// may increase performance | |
var MAKE = 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; | |
//return method; | |
var text = method.toString(); | |
var count = dependencies ? dependencies.length : 0; | |
if (count <= 0) { | |
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; | |
return Functor (text.substr(argStart, argDiff), '"use strict";\n' + text.substr(bodyStart, bodyDiff)); | |
} else { | |
var statement = "return " + text.replace('{', '{"use strict";\n'); | |
var declarations = Array(count + 1); // force collection to be most memory-consumptive at initialization | |
var dependency; | |
var symbol = 0; | |
var value = 1; | |
var meta = 2; | |
var index = 0; | |
for (index = 0; index < count; index++) { | |
dependency = dependencies[index]; | |
dependencies[index] = dependency[value]; | |
declarations[index] = dependency[symbol]; | |
} | |
declarations[count] = statement; | |
var closure = Functor.apply(null, declarations); | |
return closure.apply(null, dependencies); | |
} | |
}; | |
// 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 = MAKE(null, [["z", z]], function (x, y) { | |
z.$$ = -z.$$; | |
return x + y + z.$$; | |
}); | |
var f = MAKE(null, [["add", add]], function () { | |
var sum = 0; | |
var plus = add; | |
for (var i = 0; i < 10000; i++) { | |
sum = plus(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); // 3.62s+ | |
//}) (); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment