Skip to content

Instantly share code, notes, and snippets.

@erayarslan
Created November 10, 2016 07:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erayarslan/f0fdb670ac769d0e6919dd83f8904cb0 to your computer and use it in GitHub Desktop.
Save erayarslan/f0fdb670ac769d0e6919dd83f8904cb0 to your computer and use it in GitHub Desktop.
reflex.js
// reflex.js 0.0.1
// (c) 2014 Eray Arslan
// reflex may be freely distributed under the MIT license.
//
// fast || smooth , calculation <3 javascript || node.js
var reflex = function() {
var params = [];
for(var i = 0; i < arguments.length; i++) {
params.push(arguments[i]);
} var searchResult = reflex.prototype.search(this, params);
if(searchResult != -1) {
return reflex.prototype.brain[searchResult].r;
} var result = this.apply(this, params);
reflex.prototype.brain.push({
f : this,
p : params,
r : result
}); return result;
};
reflex.prototype = {
brain : [],
equalArray : function(t, d) {
if(t.length === d.length) {
for(var i = 0; i < t.length; i++) {
if(t[i] !== d[i]) {
return false;
}
}
} else {
return false;
} return true;
},
search : function(f, p) {
for(var i = 0; i < reflex.prototype.brain.length; i++) {
if(reflex.prototype.brain[i].f === f && reflex.prototype.equalArray(reflex.prototype.brain[i].p, p)) {
return i;
}
} return -1;
}
};
Function.constructor.prototype.reflex = reflex;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment