Skip to content

Instantly share code, notes, and snippets.

View matix's full-sized avatar

Matias Figueroa matix

  • Eventbrite
  • San Francisco, CA
View GitHub Profile
@matix
matix / benchmark.js
Created December 5, 2011 19:29
Simplest benchmarking utility function
function benchmark(fn, id, it){
id = id || new Date().toString();
it = it || 100000;
if(typeof fn == "function"){
console.time(id);
for (var i = 0; i< it; i++) fn();
console.timeEnd(id);
}
}
@matix
matix / desync-usage-desynchronize.js
Created December 13, 2012 18:58
desync() function utility. Desynchronize functions so they run after the current execution stack. Also provide a "timeout" to throttle the function. Throttle = avoid calling the function until certain time has passed, and reset that time everytime the function is called again, useful for performance improvements over very jumpy events.
function changeAllAnchorColor(color){
var anchors = document.body.getElementsByTagName("a");
for (var i = 0, l = anchors.length; i<l; i++){
anchors[i].style.color = color;
}
}
var changeAllAnchorColorDesynched = desync(changeAllAnchorColor);
document.body.innerHTML = "<div id=container><a href=#>LOL This code does nothing...</a></div>";
@matix
matix / 01-js-scoping-problem.js
Last active June 1, 2022 21:46 — forked from anonymous/Array.forEach.js
Scoping and variable hoisting are two of the most challenging subjects to learn when picking up javascript, because it is fundamentally different from the other C-like languages' behavior. Javascript, unlike C and others, creates a new variables scope for each CLOSURE created, not for each BLOCK. This leads to some confusion in some cases. Here'…
var array = ["zero","first","second","third","fourth","fifth"];
var closures = [];
for(var i=0, l=array.length; i<l; i++){
closures.push(function(){return array[i]});
}
closures[3]();
// Expected "third", but actually returns 'undefined',
// because js did not create a separate scope for the "for" block