Started Collection benchmarks
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 08ada90 · Browse files · 3 files·+121 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+121 added−0 removed
- other:+121 added−0 removed
Provenance
- Commit
dba488d- Tree
119275f- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
08ada90
3 files changed+121 added−0 removed
added:benchmarks/benchmark.js+83 added
1+2+;(function(){3+ var currentSuite4+ 5+ /**6+ * Contents of _fn_. Strips function literal and signature.7+ *8+ * @param {function} fn9+ * @return {string}10+ * @api private11+ */12+ 13+ function contentsOf(fn) {14+ return fn.toString().match(/^[^\{]*{((.*\n*)*)}/m)[1]15+ }16+ 17+ /**18+ * Pad _str_ to _len_.19+ *20+ * @param {string} str21+ * @param {integer} len22+ * @return {string}23+ * @api private24+ */25+ 26+ function pad(str, len) {27+ return str + (new Array(len - str.length)).join(' ')28+ }29+ 30+ /**31+ * Time the execution of _fn_32+ *33+ * @param {function} fn34+ * @return {float}35+ * @api private36+ */37+ 38+ function time(fn) {39+ var start = Number(new Date)40+ fn()41+ return (Number(new Date) - start) / 100042+ }43+ 44+ /**45+ * Benchmark _fn_ with the given _label_.46+ *47+ * @param {string} label48+ * @param {function} fn49+ * @api public50+ */51+ 52+ function benchmark(label, fn) {53+ var duration = time(function(){54+ for (var i = 0; i < currentSuite.times; ++i)55+ fn()56+ }).toFixed(3)57+ print(pad(' ' + label, 50 - duration.toString().length) + duration + ' |')58+ }59+ 60+ /**61+ * Create a benchmark suite with the given _label_, which62+ * will run each benchmark n _times_. If _times_ is omitted63+ * then it defaults to 1.64+ *65+ * @param {string} label66+ * @param {integer, function} times67+ * @param {function} fn68+ * @api public69+ */70+ 71+ suite = function(label, times, fn) {72+ currentSuite = this73+ if (typeof times == 'function')74+ this.times = 1, fn = times75+ else76+ this.times = times77+ print('\n ' + pad(label, 42 - this.times.toString().length) + this.times + ' time(s)')78+ print(' -------------------------------------------------')79+ eval(contentsOf(fn))80+ print('')81+ }82+ 83+})()added:benchmarks/collection.js+35 added
1234567891011121314151617181920212223242526272829303132333435
require.paths.unshift('lib')require.paths.unshift('benchmarks')process.mixin(GLOBAL, require('sys'))process.mixin(GLOBAL, require('benchmark'))require('express')
print = puts
range = function(a, b) { var array = [] while (a++ < b) array.push(a-1) return array}
suite('Collection with array', 100, function(){ array = range(0, 50000) benchmark('for', function(){ for (var i = 0, len = array.length; i < len; ++i) ; }) benchmark('for uncached', function(){ for (var i = 0; i < array.length; ++i) ; }) benchmark('forEach', function(){ array.forEach(function(){}) }) benchmark('#each', function(){ $(array).each(function(){}) })})No newline at end of filemodified:Makefile+3 added
5 unmodified lines6 7 app:8 @$(NODE) examples/app.js9 10+benchmark:11+ @$(NODE) benchmarks/collection.js12+ 13 .PHONY: test