Skip to content

Instantly share code, notes, and snippets.

@chowey
chowey / child.js
Created January 18, 2012 03:33
Programmatically get a SyntaxError lineno from Node
var fn = '\n var o={a: "Hello";\n b:"World"};'
var child = require('child_process').spawn(process.execPath, ['-e', fn]);
child.stderr.setEncoding('utf8');
child.stderr.on('data', function (data) {
var errLines = data.split('\n');
var infoLine = errLines[1].split(':');
var descLine = errLines[4];
@chowey
chowey / syntax.js
Created February 18, 2012 22:32
Jade Syntax Checker
var jade = require('jade'),
runtime = require('jade/lib/runtime'),
spawn = require('child_process').spawn,
fs = require('fs');
function parse(str, filename){
var options = {filename: filename, compileDebug: true};
try {
// Parse
var parser = new jade.Parser(str, filename, options)
@chowey
chowey / bench.js
Created February 23, 2012 14:37
Benchmark Multiline Stylus
var stylus1 = require('../node_modules/stylus'),
stylus2 = require('../cloned/stylus'),
Benchmark = require('benchmark'),
fs = require('fs');
fs.readFile('C:/node/cloned/stylus/test/cases/regression.127.styl', 'utf8', function (err, str) {
if (err) throw err;
var handler = function (err, css) {
if (err) console.error(err);
@chowey
chowey / transact.js
Created March 28, 2012 13:17
pg atomic transaction test
var pg = require('pg');
var client = new pg.Client('pg://user:pass@127.0.0.1/postgres');
client.connect(function () {
client.query('CREATE TEMP TABLE foo (bar real)', function () {
for (var i = 0; i < 5; i++)
insertRandomNumber();
});
client.once('drain', function () {
client.query('SELECT bar FROM foo', function (err, results) {
@chowey
chowey / transact2.js
Created March 28, 2012 13:46
pg atomic transaction test 2
var pg = require('pg'),
path = require('path'),
Query = require(path.join(path.dirname(require.resolve('pg')), 'query'));
pg.Client.prototype.next = function (config, values, callback) {
//can take in strings or config objects
config = (typeof(config) == 'string') ? { text: config } : config;
if (this.binary && !('binary' in config)) {
config.binary = true;
}
var Benchmark = require('benchmark'),
jade = require('../'),
fs = require('fs');
// Test cases
var cases = fs.readdirSync('cases').filter(function(file){
return ~file.indexOf('.jade');
}),
pretty = {},
@chowey
chowey / bench.js
Created April 20, 2012 13:08
Benchmark str concatenation
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite,
a, b;
suite
.add('push', function () {
a = [];
for (var i = 0; i < 100; i++)
a.push('Text\n');
@chowey
chowey / jade.js
Created April 25, 2012 00:44
Testing jade
/*!
* Jade
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
/**
* Module dependencies.
*/
@chowey
chowey / JadeAttrsBench.js
Created May 21, 2012 01:19 — forked from saabi/JadeTmpltBench.js
Simple optimization of Jade attrs
self =
{
header: "Header",
header2: "Header2",
header3: "Header3",
header4: "Header4",
header5: "Header5",
header6: "Header6",
list: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
};
@chowey
chowey / alert.cpp
Last active August 29, 2015 14:00
C++ Win32 AlertIfError
void AlertIfError() {
DWORD dw = GetLastError();
if (dw == ERROR_SUCCESS) {
return;
}
LPTSTR lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |