Skip to content

Instantly share code, notes, and snippets.

@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+$/, "");
@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 / 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 / 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 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 / 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 / admin.py
Created March 14, 2011 00:31
Adding Dojo's rich editor to Django's Admin.
# Example how to add rich editor capabilities to your models in admin.
from django.contrib.admin import site, ModelAdmin
import models
# we define our resources to add to admin pages
class CommonMedia:
js = (
'https://ajax.googleapis.com/ajax/libs/dojo/1.6.0/dojo/dojo.xd.js',
@uhop
uhop / perf.js
Created August 11, 2011 23:33
What's faster: ?:, Math.min(), or Math.max()?
// What's faster: "if", ?:, Math.min(), or Math.max()?
this.group(
"Min/max comparison",
function baseline(){
// this one does nothing, it is here only for comparison
var a = Math.random(), b = Math.random(),
c = a;
},
function if_stmt(){
@uhop
uhop / perf.js
Created August 12, 2011 00:28
Compare ++, --, +=, -=, = +, and = -, with different values.
// Compare ++, --, +=, -=, = +, and = -, with different values.
var a = 1, p1 = 1, m1 = -1, p9 = 9, m9 = -9;
this.group(
"inc/dec by 1",
function baseline() { return a; },
function prefix_plus_plus() { ++a; return a; },
function postfix_plus_plus() { a++; return a; },
function prefix_minus_minus() { --a; return a; },
@uhop
uhop / perf.js
Created August 12, 2011 00:44
Compare +=, -=, = +, and = -, with 9.
// Compare +=, -=, = +, and = -, with 9.
var a = 1, p9 = 9, m9 = -9;
this.group(
"inc/dec by 9 (var)",
function eq_minus_v9() { a = a - m9; return a; },
function eq_plus_v9() { a = a + p9; return a; },
function minus_eq_v9() { a -= m9; return a; },
function plus_eq_v9() { a += p9; return a; }