Browse files

Use Benchmark.js for performance testing.

  • Loading branch information...
1 parent b184a6f commit 5e1c78fe4d793c064add39d14014a6a02a2519e8 @mathiasbynens mathiasbynens committed Mar 17, 2011
Showing with 102 additions and 79 deletions.
  1. +6 −0 .gitmodules
  2. +12 −10 test-build-hash.js
  3. +12 −10 test-build-string.js
  4. +12 −10 test-build-trie.js
  5. +19 −15 test-find-hash.js
  6. +18 −15 test-find-string.js
  7. +18 −15 test-find-trie.js
  8. +4 −4 util.js
  9. +1 −0 vendor/Benchmark.js
View
6 .gitmodules
@@ -0,0 +1,6 @@
+[submodule "Benchmark.js"]
+ path = Benchmark.js
+ url = git@github.com:mathiasbynens/Benchmark.js.git
+[submodule "vendor/Benchmark.js"]
+ path = vendor/Benchmark.js
+ url = git@github.com:mathiasbynens/Benchmark.js.git
View
22 test-build-hash.js
@@ -1,16 +1,18 @@
var fs = require("fs"),
- util = require("./util.js");
+ util = require("./util.js"),
+ Benchmark = require("benchmark"),
+ suite = new Benchmark.Suite;
fs.readFile( "dict/string.txt", "utf8", function( err, data ) {
+
var all = [];
-
- var start = (new Date).getTime();
-
- for ( var i = 0; i < 100; i++ ) {
+
+ suite.add('Build hash', function() {
all.push( util.buildHashDict( data ) );
- }
-
- console.log( (new Date).getTime() - start );
-
- while(true){}
+ })
+ .on('cycle', function(bench) {
+ console.log(String(bench));
+ })
+ .run(true);
+
});
View
22 test-build-string.js
@@ -1,16 +1,18 @@
var fs = require("fs"),
- util = require("./util.js");
+ util = require("./util.js"),
+ Benchmark = require("benchmark"),
+ suite = new Benchmark.Suite;
fs.readFile( "dict/string.txt", "utf8", function( err, data ) {
+
var all = [];
-
- var start = (new Date).getTime();
-
- for ( var i = 0; i < 100; i++ ) {
+
+ suite.add('Build string', function() {
all.push( util.buildStringDict( data ) );
- }
-
- console.log( (new Date).getTime() - start );
-
- while(true){}
+ })
+ .on('cycle', function(bench) {
+ console.log(String(bench));
+ })
+ .run(true);
+
});
View
22 test-build-trie.js
@@ -1,16 +1,18 @@
var fs = require("fs"),
- util = require("./util.js");
+ util = require("./util.js"),
+ Benchmark = require("benchmark"),
+ suite = new Benchmark.Suite;
fs.readFile( "dict/suffix.js", "utf8", function( err, data ) {
+
var all = [];
-
- var start = (new Date).getTime();
-
- for ( var i = 0; i < 100; i++ ) {
+
+ suite.add('Build trie', function() {
all.push( util.buildTrie( data ) );
- }
-
- console.log( (new Date).getTime() - start );
-
- while(true){}
+ })
+ .on('cycle', function(bench) {
+ console.log(String(bench));
+ })
+ .run(true);
+
});
View
34 test-find-hash.js
@@ -1,26 +1,30 @@
var fs = require("fs"),
- util = require("./util.js");
+ util = require("./util.js"),
+ Benchmark = require("benchmark"),
+ suite = new Benchmark.Suite;
fs.readFile( "dict/string.txt", "utf8", function( err, data ) {
+
var words = data.split(" ");
fs.readFile( "dict/string.js", "utf8", function( err, data ) {
util.buildHashDict( data );
-
- var start = (new Date).getTime();
-
- for ( var i = 0, l = words.length; i < l; i++ ) {
- util.findHashWord( words[i] );
- }
-
- console.log( (new Date).getTime() - start );
- start = (new Date).getTime();
+ suite.add(function() {
+ for ( var i = 0, l = words.length; i < l; i++ ) {
+ util.findHashWord( words[i] );
+ }
+ })
+ .add(function() {
+ for ( var i = 0, l = words.length; i < l; i++ ) {
+ util.findHashWord( words[i] + "z" );
+ }
+ })
+ .on('cycle', function(bench) {
+ console.log(String(bench));
+ })
+ .run(true);
- for ( var i = 0, l = words.length; i < l; i++ ) {
- util.findHashWord( words[i] + "z" );
- }
-
- console.log( (new Date).getTime() - start );
});
+
});
View
33 test-find-string.js
@@ -1,26 +1,29 @@
var fs = require("fs"),
- util = require("./util.js");
+ util = require("./util.js"),
+ Benchmark = require("benchmark"),
+ suite = new Benchmark.Suite;
fs.readFile( "dict/string.txt", "utf8", function( err, data ) {
+
var words = data.split(" ");
fs.readFile( "dict/string.js", "utf8", function( err, data ) {
util.buildStringDict( data );
-
- var start = (new Date).getTime();
-
- for ( var i = 0, l = words.length; i < l; i++ ) {
- util.findStringWord( words[i] );
- }
-
- console.log( (new Date).getTime() - start );
-
- start = (new Date).getTime();
- for ( var i = 0, l = words.length; i < l; i++ ) {
- util.findStringWord( words[i] + "z" );
- }
+ suite.add(function() {
+ for ( var i = 0, l = words.length; i < l; i++ ) {
+ util.findStringWord( words[i] );
+ }
+ })
+ .add(function() {
+ for ( var i = 0, l = words.length; i < l; i++ ) {
+ util.findStringWord( words[i] + "z" );
+ }
+ })
+ .on('cycle', function(bench) {
+ console.log(String(bench));
+ })
+ .run(true);
- console.log( (new Date).getTime() - start );
});
});
View
33 test-find-trie.js
@@ -1,26 +1,29 @@
var fs = require("fs"),
- util = require("./util.js");
+ util = require("./util.js"),
+ Benchmark = require("benchmark"),
+ suite = new Benchmark.Suite;
fs.readFile( "dict/string.txt", "utf8", function( err, data ) {
+
var words = data.split(" ");
fs.readFile( "dict/suffix.js", "utf8", function( err, data ) {
util.buildTrie( data );
- var start = (new Date).getTime();
-
- for ( var i = 0, l = words.length; i < l; i++ ) {
- util.findTrieWord( words[i] );
- }
-
- console.log( (new Date).getTime() - start );
-
- start = (new Date).getTime();
-
- for ( var i = 0, l = words.length; i < l; i++ ) {
- util.findTrieWord( words[i] + "z" );
- }
+ suite.add(function() {
+ for ( var i = 0, l = words.length; i < l; i++ ) {
+ util.findTrieWord( words[i] );
+ }
+ })
+ .add(function() {
+ for ( var i = 0, l = words.length; i < l; i++ ) {
+ util.findTrieWord( words[i] + "z" );
+ }
+ })
+ .on('cycle', function(bench) {
+ console.log(String(bench));
+ })
+ .run(true);
- console.log( (new Date).getTime() - start );
});
});
View
8 util.js
@@ -7,21 +7,21 @@ exports.buildTrie = function( txt ) {
exports.buildStringDict = function( txt ) {
return (dict = " " + txt + " ");
};
-
+
exports.buildHashDict = function( txt ) {
var words = txt.split(" "),
tmp = {};
for ( var i = 0, l = words.length; i < l; i++ ) {
tmp[ words[i] ] = true;
}
-
+
return (dict = tmp);
};
exports.findTrieWord = function findTrieWord( word, cur ) {
cur = cur || dict;
-
+
for ( var node in cur ) {
if ( word.indexOf( node ) === 0 ) {
var val = typeof cur[ node ] === "number" && cur[ node ] ?
@@ -36,7 +36,7 @@ exports.findTrieWord = function findTrieWord( word, cur ) {
}
}
}
-
+
return false;
};
1 vendor/Benchmark.js
@@ -0,0 +1 @@
+Subproject commit c1ec358faf483999e52dfe1e41c9dad3b471c9f6

0 comments on commit 5e1c78f

Please sign in to comment.