Created
October 16, 2012 03:45
-
-
Save tagomoris/3897132 to your computer and use it in GitHub Desktop.
node.js html parser (jquery style) benchmark
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
var fs = require('fs'), | |
async = require('async'); | |
var try_series = function(func_name, func, data, times, cb){ | |
var start = new Date(); | |
var tries = new Array(times); | |
for(var i = 0 ; i < times ; i++) { | |
tries[i] = function(callback){func(data, callback);}; | |
} | |
async.series(tries, function(err,result){ | |
var end = new Date(); | |
console.log(func_name + ':' + (end - start) + ' in ' + times + ' times.'); | |
cb(null, result); | |
}); | |
}; | |
var jquery = fs.readFileSync("./jquery-1.8.2.min.js").toString(); | |
var jsdom_parse = function(data, callback){ | |
var jsdom = require("jsdom"); | |
jsdom.env({ | |
html: data, | |
src: [jquery], | |
done: function(errors, window){ | |
var $ = window.$; | |
callback(null, $('#01-01').attr('class')); | |
} | |
}); | |
}; | |
var jquery_parse = function(data, callback){ | |
var $ = require('jQuery'); | |
$(data); | |
callback(null, $('#01-01').attr('class')); | |
}; | |
var cheerio_parse = function(data, callback){ | |
var cheerio = require('cheerio'), | |
$ = cheerio.load(data); | |
callback(null, $('#01-01').attr('class')); | |
}; | |
var text = fs.readFileSync('./target.html').toString(); | |
var times = 100; | |
async.series([ | |
function(cb){try_series('jsdom', jsdom_parse, text, times, cb);}, | |
function(cb){try_series('jQuery', jquery_parse, text, times, cb);}, | |
function(cb){try_series('cheerio', cheerio_parse, text, times, cb);} | |
], function(err,result){ | |
console.log({err:err, results:result.map(function(d){return d.length;})}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment