I don't think a global variable lookup is the reason for the difference. Here is the code that jsperf generates for the function version of the test:
(Benchmark.uid1400600789397runScript || function() {})();
Benchmark.uid1400600789397createFunction = function(window, t14006007893970) {
var global = window,
clearTimeout = global.clearTimeout,
setTimeout = global.setTimeout;
var r14006007893970, s14006007893970, m14006007893970 = this,
f14006007893970 = m14006007893970.fn,
i14006007893970 = m14006007893970.count,
n14006007893970 = t14006007893970.ns;
// Test Setup
var makeKeyCodepoint = 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
);
};
s14006007893970 = n14006007893970.now();
while (i14006007893970--) {
// Test Code
var key;
key = makeKeyCodepoint('www.wired.com');
key = makeKeyCodepoint('www.youtube.com');
key = makeKeyCodepoint('scorecardresearch.com');
key = makeKeyCodepoint('www.google-analytics.com');
}
r14006007893970 = (n14006007893970.now() - s14006007893970) / 1e3;
return {
elapsed: r14006007893970,
uid: "uid14006007893970"
}
}
The test setup and the test itself are all part of the same function, and makeKeyCodepoint is a local variable in that function.