获得性能编写相同的代码?

ale*_*nst 0 javascript performance node.js

我想知道是否

function pathJoin(uri,file){
    return url.format(
        url.parse(
            path.normalize(
                path.join(uri, file) 
            ).split(
                path.delimiter
            ).join("/")
        )
    );
}
Run Code Online (Sandbox Code Playgroud)

function pathJoin(uri,file){
    var joined_path = path.join(uri, file);
    var normalized = path.normalize( joined_path );
    var splitted = normalized.split(path.delimiter);
    var joined = splitted.join("/");
    var parsed = url.parse(joined);
    return url.format(parsed);
}
Run Code Online (Sandbox Code Playgroud)

表现同样快.多次从功能切换到功能是否有任何惩罚?

Gab*_*mas 5

注意:path.join已经返回一个规范化路径.它们具有相同的性能......并且不要使用jsperf来对节点进行基准测试.

var path = require ("path");
var url = require ("url");
var speedy = require ("speedy");

function pathJoin(uri,file){
    return url.format(
        url.parse(
            path.normalize(
                path.join(uri, file) 
            ).split(
                path.delimiter
            ).join("/")
        )
    );
}

function pathJoin2(uri,file){
    var joined_path = path.join(uri, file);
    var normalized = path.normalize( joined_path );
    var splitted = normalized.split(path.delimiter);
    var joined = splitted.join("/");
    var parsed = url.parse(joined);
    return url.format(parsed);
}

speedy.timeout (20000);
speedy.run ({
    "1": function (){
        pathJoin ("http://www.google.com", "file");
    },
    "2": function (){
        pathJoin2 ("http://www.google.com", "file");
    }
})
Run Code Online (Sandbox Code Playgroud)

结果

File: t.js

Node v0.10.20
V8 v3.14.5.9
Speedy v0.0.8

Benchmarks: 2
Timeout: 20000ms (20s 0ms)
Samples: 3
Total time per benchmark: ~60000ms (1m 0s 0ms)
Total time: ~120000ms (2m 0s 0ms)

Higher is better (ops/sec)

1
  50,931 ± 0.1%
2
  51,029 ± 0.1%

Elapsed time: 120063ms (2m 0s 63ms)
Run Code Online (Sandbox Code Playgroud)