Skip to content

Commit

Permalink
Initial commit! Works as far as I can tell.
Browse files Browse the repository at this point in the history
  • Loading branch information
appden committed Aug 18, 2009
0 parents commit 5d56b17
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
@@ -0,0 +1,8 @@
Speed
=====

_Speed_ is a crude JavaScript speed testing framework. It makes it easy to test various techniques to see which is the fastest. The output is displayed in the console; if there isn't one, then Firebug Lite is used. If you have a **Need for Speed**, then check this out and help me make it better.

### Documentation

There is none.
105 changes: 105 additions & 0 deletions Speed.js
@@ -0,0 +1,105 @@

(function(){

var required = {};
var loading = [];
var callbacks = [];
var ready = true;

var require = function(files, callback){
if (!files) return;
if (typeof files == 'string') files = [files];

var src;
while (src = files.shift()){
if (required[src]) continue;
required[src] = true;
loading.push(src);
}

if (ready) next();
push(callback);
};

var next = function(){
ready = !loading.length;
if (ready){
var callback;
while (callback = callbacks.shift()) callback();
} else {
load(loading.shift(), next);
}
};

var push = function(callback){
if (!callback) return;
if (ready) callback();
else callbacks.push(callback);
};

var head = document.getElementsByTagName('head')[0];

var load = function(src, callback){
var script = document.createElement('script');
script.type = 'text/javascript';

if (script.readyState) script.onreadystatechange = function(){
if (script.readyState == 'loaded' || script.readyState == 'complete'){
script.onreadystatechange = null;
if (callback) callback();
}
};
else script.onload = callback;

script.src = src;
head.appendChild(script);
};

if (!console || !console.time)
require('http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js');

var run = function(name, test, iterations){
console.group(name);

iterations = test.iterations || iterations;
var setup = test.setup, teardown = test.teardown, before = test.before, after = test.after;
delete test.iterations; delete test.setup; delete test.teardown; delete test.before; delete test.after;

var exec = function(fn){
if (fn) fn.call(test);
};

exec(setup);

for (var type in test){
var fn = test[type];
if (typeof fn != 'function') continue;

exec(before);
console.time(type);

for (var i = iterations; i--; ) fn.call(test);

console.timeEnd(type);
exec(after);
}

exec(teardown);
console.groupEnd(name);
};

this.Speed = function(tests){
require(tests.require);
push(tests.setup);
var iterations = tests.iterations || 100;

delete tests.require; delete tests.setup; delete tests.iterations;

push(function(){
for (var name in tests) run(name, tests[name], iterations);
});
};

Speed.require = require;

})();
11 changes: 11 additions & 0 deletions index.html
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Speed</title>
<script src="Speed.js" type="text/javascript"></script>
<script src="tests.js" type="text/javascript"></script>
</head>
<body>
<h1>Speed</h1>
</body>
</html>
36 changes: 36 additions & 0 deletions tests.js
@@ -0,0 +1,36 @@

Speed({

require: 'http://ajax.googleapis.com/ajax/libs/mootools/1.2.3/mootools-yui-compressed.js',

'Child Selector': {

setup: function(){
var append = function(div, depth){
if (!depth) return;
for (var i = 10; i--; )
append(new Element('div').inject(div), depth - 1);
};
append(this.div = new Element('div'), 2);
},

'getElementsByTagName': function(){
var els = this.div.getElementsByTagName('div');
var children = [];
for (var i = els.length; i--; ){
if (els[i].parentNode === this.div) children.push(els[i]);
}
},

'firstChild': function(){
var children = [];
var el = this.div.firstChild;
do {
if (el.tagName.toLowerCase() == 'div')
children.push(el);
} while (el = el.nextSibling);
}

}

});

0 comments on commit 5d56b17

Please sign in to comment.