Reputation: 3854
See code and benchmark here: http://jsperf.com/single-vs-multiple-times-2
I am seeing a strange behaviour in javascript. Running code single time is taking twice as much as running it multiple times. Following is the code:
Preparation code
var MakeKeyCodepoint = function() {};
MakeKeyCodepoint.prototype.makeKey = function(word) {
var len = word.length;
if (len > 255) {
return undefined;
}
var i = len >> 2;
return String.fromCharCode(
(word.charCodeAt(0) & 0x03) << 14 |
(word.charCodeAt(i) & 0x03) << 12 |
(word.charCodeAt(i + i) & 0x03) << 10 |
(word.charCodeAt(i + i + i) & 0x03) << 8 |
len
);
};
var makeKeyCodepointObj = new MakeKeyCodepoint();
Running single time
var key = makeKeyCodepointObj.makeKey('www.wired.com');
Running four times
var key = makeKeyCodepointObj.makeKey('www.wired.com');
key = makeKeyCodepointObj.makeKey('www.youtube.com');
key = makeKeyCodepointObj.makeKey('scorecardresearch.com');
key = makeKeyCodepointObj.makeKey('www.google-analytics.com');
I am running it in chrome 34 in osx.
Upvotes: 0
Views: 156
Reputation: 140220
You need to fix your benchmark so that it actually does something: http://jsperf.com/single-vs-multiple-times-2/5
Should give exactly 4 times slower.
Upvotes: 0
Reputation: 3755
looks like you are benchmarking the operations per second, not the time for completion. running it more times at onces would have more operations to perform so your results seem reasonable
reviewing the revisions i would say that was an anomaly run and the result was likely longer due to website server response when you ran it
Revision 4 shows what i would expect to see
Upvotes: 1