numbers.js sum
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
var numbers = {};
numbers.sum = function (arr) {
if (Object.prototype.toString.call(arr) === '[object Array]') {
var total = 0;
for (var i = 0 ; i < arr.length ; i++) {
if (typeof(arr[i]) === 'number') {
total = total + arr[i];
} else {
throw new Error('All elements in array must be numbers');
}
}
return total;
} else {
throw new Error('Input must be of type Array');
}
};
var confident = {};
confident.sum = function (arr) {
var total = 0;
for (var i = 0, l = arr.length; i < l; i++) {
total += arr[i];
}
return total;
};
var a = [];
for (var i = 0; i < 10000; i++) {
a.push(Math.round(Math.random() * 100));
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
numbers |
|
pending… |
confident |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published
0 comments