Buy/Sell
JavaScript performance comparison
Info
Given an array of price points what's the fastest way to find the best point at which to buy and sell?
Preparation code
<script>
Benchmark.prototype.setup = function() {
var entropy = 1000;
var points = Array(entropy);
for (i = 0; i < points.length; i++) {
points[i] = Math.floor(Math.random() * entropy);
if (i > 0) {
if (Math.round(Math.random() % 2)) { // sometimes add
points[i] = points[i - 1] + points[i];
} else { // sometimes subtract
points[i] = points[i - 1] - points[i];
}
// don't go into the red
points[i] = Math.abs(points[i]);
} else {
// start from a random point
points[i] = Math.round(Math.random() * entropy * entropy);
}
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Highest and Lowest Subsequent Points |
|
pending… |
Iterative |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments