I've played with jsperf.com and found that prototyped function is 40x slower than "default" declared function.String.prototype.contains = function(s){ return !!~this.indexOf(s) } = 220K ops/svs.function isContains(str, s) { return !!~str.indexOf(s) } = 8.5KK ops/sHere's a jsperf test caseP.S. I know that prototype modification isn't the best case and can be named 'monkey patching' :) 解决方案 I think it is slow because the string primitive is automatically wrapped with a temporary object each time a method is called.This also explains the performance boost of new Object("hi").foo() over "hi".foo().From the MDN docs:Nearby:Why can't I add properties to a string object in javascript?String object versus literal - modifying the prototype? 这篇关于为什么原型函数比默认声明的函数慢40倍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-19 23:54