View formatNumber.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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+$/, ""); |
View perf.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); } |
View perf.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; } | |
}, |
View Evolution of a Python programmer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Newbie programmer | |
def factorial(x): | |
if x == 0: | |
return 1 | |
else: | |
return x * factorial(x - 1) | |
print factorial(6) | |
#First year programmer, studied Pascal |
View perf.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(){}; |
View perf.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// chaining functions | |
var flag = 0; | |
var list = [ | |
function(f){}, | |
function(f){}, | |
function(f){}, | |
function(f){ flag = f; } | |
]; |
View admin.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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', |
View perf.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(){ |
View perf.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; }, |
View perf.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; } |
OlderNewer