【发布时间】:2012-03-02 04:40:55
【问题描述】:
我在 jsperf.com 上玩过,发现原型函数比“默认”声明的函数慢 40 倍。
String.prototype.contains = function(s){ return !!~this.indexOf(s) } = 220K 操作/秒
对比
function isContains(str, s) { return !!~str.indexOf(s) } = 8.5KK ops/s
附:我知道原型修改不是最好的情况,可以命名为“猴子补丁”:)
【问题讨论】:
-
检查一下,它会影响字符串和数字文字,但不会影响数组文字 - jsperf.com/shoop-da-whoop
-
仅在将原型添加到已知类时。 stackoverflow.com/questions/32847086/…
标签: javascript performance prototype