6

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/s

vs.

function isContains(str, s) { return !!~str.indexOf(s) } = 8.5KK ops/s

Here's a jsperf test case

P.S. I know that prototype modification isn't the best case and can be named 'monkey patching' :)

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
mjey
  • 183
  • 2
  • 9
  • Check this out, it affects string and number literals but not array literals - http://jsperf.com/shoop-da-whoop – Dagg Nabbit Mar 02 '12 at 05:38
  • only when adding prototype to known Classes. http://stackoverflow.com/questions/32847086/javascript-performance-of-extending-string-prototype-is-abysmal-vs-static-funct – Omeriko Oct 29 '15 at 13:56

2 Answers2

6

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:

String literals (denoted by double or single quotes) and strings returned from String calls in a non-constructor context (i.e., without using the new keyword) are primitive strings. JavaScript automatically converts primitives and String objects, so that it's possible to use String object methods for primitive strings. In contexts where a method is to be invoked on a primitive string or a property lookup occurs, JavaScript will automatically wrap the string primitive and call the method or perform the property lookup.

Nearby:

Why can't I add properties to a string object in javascript?

String object versus literal - modifying the prototype?

Community
  • 1
  • 1
Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
  • Sorry for the off-topic! But, you made a thingie called [`decl`](http://gitbuh.github.com/decl/) which I liked, and while I was playing around with it I noticed it doesn't seem to run on IE8 (either that, or the `detest` harness (cool name!) doesn't work, [the page](http://gitbuh.github.com/decl/site/test.html) looks weird on IE8). So yeah, I made a chat room in case you might have a few moments to tell me what might be the cause (I'm willing to hack it around), and to not pollute this comment area. [Here](http://chat.stackoverflow.com/rooms/17017/room-for-camilo-martin-and-ggg). And kudos :) – Camilo Martin Sep 23 '12 at 11:34
1

Odds are very good that you're replacing a primitive function implemented in C/machine code with a JavaScript body in the monkey-patched version.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
  • 1
    I don't get what you mean. `"".contains` doesn't exist... what native function do you think he is shadowing? – Dagg Nabbit Mar 02 '12 at 04:49
  • My guess is that the prototype dispatch is thwarting the implementation's ability to get some efficient version of indexOf(). The magic there is thed 40x difference -- that smells of interpretation vs compiled code. – Charlie Martin Mar 02 '12 at 04:55
  • How would you explain CMS' revision 4 and my revision 5 of the perf? I'm totally at a loss here. – Dagg Nabbit Mar 02 '12 at 05:09
  • @CharlieMartin correct me if I'm wrong, but I think "proto outer" kind of disagrees with that theory here: http://jsperf.com/outer-fn-vs-prototype/8 – Shea Mar 02 '12 at 05:45
  • @mjey I agree, but I deleted that answer. I think there might be some kind of behind the scenes conversions going on here with "this", happening multiple times. – Shea Mar 02 '12 at 05:50
  • @mjey `this` did seem to slow things down a bit in my tests, but I think I realized the issue. See my answer. – Dagg Nabbit Mar 02 '12 at 06:09
  • 1
    Aren't you guys saying much what I am? Something is thwearting the efficient implementation. The prototype version, it turns out, is wrapping what would be a call to low-level code with something that wraps it up in an object, creating a new object *in the interpreter* every time. – Charlie Martin Mar 02 '12 at 18:40
  • @CharlieMartin "Something is thwarting the efficient implementation" is sort of begging the question. I didn't take from your answer that the slowness was coming from boxing the primitives. – Dagg Nabbit Mar 05 '12 at 18:47