Skip to content

Instantly share code, notes, and snippets.

@uhop
uhop / perf.js
Created January 21, 2011 00:12
Chaining functions
// chaining functions
var flag = 0;
var list = [
function(f){},
function(f){},
function(f){},
function(f){ flag = f; }
];
@uhop
uhop / perf.js
Created January 18, 2011 08:27
Creating simple objects.
// Creating simple objects.
// our constructors
var A = function(){};
var B = function(){};
B.prototype.m = function(){};
var C = function(){
this.p = 42;
};
C.prototype.m = function(){};
@uhop
uhop / Evolution of a Python programmer.py
Created November 21, 2010 20:13
Evolution of a Python programmer
#Newbie programmer
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x - 1)
print factorial(6)
#First year programmer, studied Pascal
@uhop
uhop / perf.js
Created October 21, 2010 05:35
Iterating over array
// Benchmarking summation of array
var a = [];
for(var i = 0; i < 100; ++i) a.push(Math.random());
this.group(
"Iterations",
function classic(){
for(var sum = 0, i = 0, l = a.length; i < l; ++i){ sum += a[i]; }
},
@uhop
uhop / perf.js
Created October 4, 2010 05:05
Calling function
// Benchmarking different ways to call a function.
var context = {}, fb = null;
function test(){}
this.group(
"Calling function",
function Normal(x){ return test(x); },
{
name: "Function.call()",
test: function(x){ return test.call(context, x); }
@uhop
uhop / formatNumber.js
Created June 6, 2009 02:15
Fixing Spymaster's formatNumber()
// How to format big numbers using commas to separate 1000s and abbreviations
// Example: 12345678900 => 12.34B
var numPattern = /^(\d{0,2})(\d{3})?(\d{3})?(\d{3})?(\d{3})?(\d{3})?(\d{3})?(\d{3})?(\d{3})?(\d{3})?(\d{3})?$/;
function putCommasIn(s){
return s.match(numPattern).slice(1).join(",").replace(/^\,{1,}/, "").replace(/\,{1,}$/, "");
}
function trimZeroes(s){
return s.replace(/\.?0+$/, "");