Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: micro-optimize url.resolve() #184

Merged
merged 2 commits into from
Dec 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
31 changes: 31 additions & 0 deletions benchmark/url/url-resolve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var common = require('../common.js');
var url = require('url');
var v8 = require('v8');

var bench = common.createBenchmark(main, {
type: ['one'],
n: [1e5],
});

function main(conf) {
var type = conf.type;
var n = conf.n | 0;

var inputs = {
one: ['http://example.com/', '../../../../../etc/passwd'],
};
var input = inputs[type] || [];

// Force-optimize url.resolve() so that the benchmark doesn't get
// disrupted by the optimizer kicking in halfway through.
for (var name in inputs)
url.resolve(inputs[name][0], inputs[name][1]);

v8.setFlagsFromString('--allow_natives_syntax');
eval('%OptimizeFunctionOnNextCall(url.resolve)');

bench.start();
for (var i = 0; i < n; i += 1)
url.resolve(input[0], input[1]);
bench.end(n);
}
13 changes: 10 additions & 3 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,12 +670,12 @@ Url.prototype.resolveObject = function(relative) {
for (var i = srcPath.length; i >= 0; i--) {
last = srcPath[i];
if (last === '.') {
srcPath.splice(i, 1);
spliceOne(srcPath, i);
} else if (last === '..') {
srcPath.splice(i, 1);
spliceOne(srcPath, i);
up++;
} else if (up) {
srcPath.splice(i, 1);
spliceOne(srcPath, i);
up--;
}
}
Expand Down Expand Up @@ -750,3 +750,10 @@ Url.prototype.parseHost = function() {
}
if (host) this.hostname = host;
};

// About 1.5x faster than the two-arg version of Array#splice().
function spliceOne(list, index) {
for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1)
list[i] = list[k];
list.pop();
}