Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mocha browser runner is 10-100x slower than jasmine on equivalent suites #763

Merged
merged 3 commits into from
Apr 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/reporters/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ function HTML(runner, root) {
});

runner.on('test end', function(test){
window.scrollTo(0, document.body.scrollHeight);

// TODO: add to stats
var percent = stats.tests / this.total * 100 | 0;
if (progress) progress.update(percent).draw(ctx);
Expand Down
14 changes: 10 additions & 4 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ var EventEmitter = require('events').EventEmitter
, Test = require('./test')
, utils = require('./utils')
, filter = utils.filter
, keys = utils.keys
, noop = function(){}
, immediately = global.setImmediate || process.nextTick;
, keys = utils.keys;

/**
* Non-enumerable globals.
Expand Down Expand Up @@ -62,6 +60,14 @@ function Runner(suite) {
this.globals(this.globalProps().concat(['errno']));
}

/**
* Wrapper for setImmediate, process.nextTick, or browser polyfill.
*
* @param {Function} fn
* @api private
*/
Runner.immediately = global.setImmediate || process.nextTick;

/**
* Inherit from `EventEmitter.prototype`.
*/
Expand Down Expand Up @@ -243,7 +249,7 @@ Runner.prototype.hook = function(name, fn){
});
}

immediately(function(){
Runner.immediately(function(){
next(0);
});
};
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ exports.filter = function(arr, fn){
* @api private
*/

exports.keys = function(obj) {
exports.keys = Object.keys || function(obj) {
var keys = []
, has = Object.prototype.hasOwnProperty // for `window` on <=IE8

Expand Down
53 changes: 25 additions & 28 deletions support/tail.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,6 @@ process.exit = function(status){};
process.stdout = {};
global = window;

/**
* next tick implementation.
*/

process.nextTick = (function(){
// postMessage behaves badly on IE8
if (window.ActiveXObject || !window.postMessage) {
return function(fn){ fn() };
}

// based on setZeroTimeout by David Baron
// - http://dbaron.org/log/20100309-faster-timeouts
var timeouts = []
, name = 'mocha-zero-timeout'

window.addEventListener('message', function(e){
if (e.source == window && e.data == name) {
if (e.stopPropagation) e.stopPropagation();
if (timeouts.length) timeouts.shift()();
}
}, true);

return function(fn){
timeouts.push(fn);
window.postMessage(name, '*');
}
})();

/**
* Remove uncaughtException listener.
*/
Expand Down Expand Up @@ -72,6 +44,31 @@ process.on = function(e, fn){
var Mocha = window.Mocha = require('mocha'),
mocha = window.mocha = new Mocha({ reporter: 'html' });

var immediateQueue = []
, immediateTimeout;

function timeslice() {
var immediateStart = new Date().getTime();
while (immediateQueue.length && (new Date().getTime() - immediateStart) < 100) {
immediateQueue.shift()();
}
if (immediateQueue.length) {
immediateTimeout = setTimeout(timeslice, 0);
} else {
immediateTimeout = null;
}
}

/**
* High-performance override of Runner.immediately.
*/
Mocha.Runner.immediately = function(callback) {
immediateQueue.push(callback);
if (!immediateTimeout) {
immediateTimeout = setTimeout(timeslice, 0);
}
};

/**
* Override ui to ensure that the ui functions are initialized.
* Normally this would happen in Mocha.prototype.loadFiles.
Expand Down