Skip to content

Instantly share code, notes, and snippets.

View chjj's full-sized avatar

Christopher Jeffrey (JJ) chjj

View GitHub Profile
[obj] [structure] [property] [value]
test_article none title A Test Article
test_article none content Hello world!
test_article tags 0 misc
test_article tags 1 etc
test_article comments.0 poster_name AvgJoe
test_article comments.0 content This article is amazing
test_article comments.1 poster_name SuperNerd
test_article comments.1 content No it's not!
another_article none title Another Article
@chjj
chjj / loop-bench.js
Created May 1, 2011 10:00
loop benchmarks
var a = [];
for (var i = 0; i < 100000; i++) a.push('a');
var o = {};
for (var i = 0; i < 100000; i++) o['key_' + i] = 'a';
(function() {
var s = Date.now();
var i = a.length;
while (i--) {
@chjj
chjj / joinvconcat.js
Created May 7, 2011 05:29
join v concat
(function() {
var start = Date.now();
var i = 1000000;
while (i--) {
var str = '';
str += 'a';
str += 'a';
str += 'a';
str += 'a';
str += 'a';
@chjj
chjj / calleebench.js
Created May 9, 2011 08:22
callee bench
var times = 15000;
(function() {
var i = 0, start = Date.now();
(function() {
if (++i < times) arguments.callee.call();
})();
console.log('callee', Date.now() - start);
})();
@chjj
chjj / simple_request.js
Created May 19, 2011 22:46
simple request
var http = require('http')
, parse = require('url').parse
, StringDecoder = require('string_decoder').StringDecoder;
var LIMIT = 10 * 1024;
var request = function(url, body, func) {
if (typeof url !== 'object') {
url = parse(url);
}
@chjj
chjj / base64fix.js
Created June 15, 2011 12:07
base64 cipher fix
// a shim to fix base64 cipher output, untested
var Cipher = require('crypto').Cipher;
var update = Cipher.prototype.update,
final = Cipher.prototype.final;
Cipher.prototype.update = function(data, input, output) {
if (output === 'base64') {
data = update.call(this, data, input, 'binary');
return new Buffer(data, 'binary').toString('base64');
@chjj
chjj / fastuint32.js
Created June 27, 2011 10:58
uint32write
// ignore the shims
// the original (slow) version
var Buffer1 = (function() {
var assert = {};
var fail = function() { throw new Error('assertion failed'); };
var Buffer = function() { this.length = 100; };
Buffer.prototype.writeUInt32 = function(value, offset, endian) {
var buffer = this;
@chjj
chjj / slice_args.js
Created August 9, 2011 01:46
slice arguments
(function() {
var bench = function(func, t) {
var start = new Date()
, i = t || 100000;
while (i--) func(1, 2, 3);
console.log('%s: %sms', func.name, new Date() - start);
};
var slice = Array.prototype.slice;
bench(function _slice() {
@chjj
chjj / coffeescript.poem
Created August 15, 2011 23:09
a poem about coffeescript
a poem about coffeescript in the style of the lord of the rings:
one does not simply walk into coffeescript
its syntax is guarded by more than just ruby devs
and the significant whitespace is ever present
it is a wasteland barren of curly braces
- riddled with class syntax, postfix conditionals, and operator aliases
the very code you write, is a poisonous fume
not with 10,000 men who knew javascript could you do this
@chjj
chjj / two_paragraphs.js
Created August 18, 2011 22:46
two paragraphs
var fs = require('fs')
, marked = require('marked');
var text = fs.readFileSync(__dirname + '/test.md', 'utf8');
var two = function(str) {
var tokens = marked.lexer(str)
, paragraphs = 0
, i = 0
, token;