Skip to content

Instantly share code, notes, and snippets.

@kvanbere
Last active November 21, 2017 09:39
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 kvanbere/4c2f601f00ee2905d98f945b7c1b8ad0 to your computer and use it in GitHub Desktop.
Save kvanbere/4c2f601f00ee2905d98f945b7c1b8ad0 to your computer and use it in GitHub Desktop.
// ~ 120k log op/s
(function() {
var originalArrayValueOf = Array.prototype.valueOf;
var logs = {};
var index = 1;
function hasher(str) {
var target = Array.from(str);
target.length > 16 && (target = target.slice(0, 16)); // perf: avoid alloc
return target
.reduce(function(acc, i) {
return 0 | ((acc << 5) - acc) + i.charCodeAt(0); }, 0);
}
Array.prototype.valueOf = function() {
if (this.length > 0 && typeof this[0] === 'string') {
var bucket = hasher(this[0]);
switch (bucket) {
case 57093687: // :show
console.info('$' + index,
window['$' + index++] = Array.prototype.concat.apply([], this
.slice(1)
.map(function(x) { return (logs[hasher(x)] || []).map(JSON.parse); }))
);
break;
case -904836167: // :unload
Array.prototype.valueOf = originalArrayValueOf;
break;
case 56763003: // :help
[ "Valid commands are",
" :show, bucket [, bucket2..]",
" :unload"
].map(function(s) { console.info(s); });
break;
default:
logs[bucket] === undefined && (logs[bucket] = []);
logs[bucket].push(JSON.stringify(this.slice(1)));
break;
}
}
return this;
}
})();
@kvanbere
Copy link
Author

  • config = { max_logs per bucket, log expiry ... }
  • get/set configs
  • clear a bucket or clear all logs
  • return all logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment