意外的性能问题

Unexpected performance issue

我最近写了一个 nodejs 包 अनुमार्गाक (Anumargak) 用于 HTTP 路由。我遇到了一个奇怪的性能问题。

它有一个方法 on 来注册路由和 find 来查找已注册的路由。

var anumargak = Anumargak();
anumargak.on("GET",'/some/url',fn);
anumargak.find("GET",'/some/url');

当用户调用on方法时,它首先调用find方法来检查路由是否已经注册,否则将路由信息保存在二维数组中。当用户调用 find 方法时,它只是在二维数组中寻找一个条目。

Anumargak.prototype.on = function(method,url,fn){
    if(this.find(method,url)){
      this.count --;//count of unique registered routes.
    }
    this.count +=1;
    this._on(method,url,fn);//_on method has actual logic of saving route information
}

Anumargak.prototype.find = function(method,url){
    var result = this.staticRoutes[method][url];
    if(result) return result.fn;
    else{
        //handles dynamic routes
    }
    return this.defaultFn;
}

find 方法不依赖于 on 方法。但是当我从 on 方法调用 find 方法时,find 方法的性能下降了一半。我只调用了一次 on 方法并测试了 find 方法的性能。可能的原因是什么?

我正在使用节点 v9.5.0。

我不确定性能有什么问题。但是调用 this.find() 是不正确的。因为 this.find 接受实际的 URL 而注册的 URL 可以有正则表达式或参数细节。

因此,我重写了代码来解决性能问题。