Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit! Works as far as I can tell.
- Loading branch information
0 parents
commit 5d56b17
Showing
4 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
})(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} | ||
|
||
}); |