[nodejs] A Question about V8 Javascript engine.

3 views
Skip to first unread message

Dusko Jordanovski

unread,
May 10, 2010, 5:57:23 PM5/10/10
to nodejs
I want to apologize if this question is inappropriate for this group.
What happens when I have something like this in my Node code:

function A(){
this.method = function(){ return 5; }
}

var a = new A();

Let's say we make a lot of instances of A. How big a performance issue
this is? I know I can put the method inside the prototype or define it
as an external function and assign it as a property, but I'm
interested in this particular case.

--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.

nwhite

unread,
May 10, 2010, 10:22:01 PM5/10/10
to nod...@googlegroups.com
I ran some benchmarks on my laptop, each test was looped 500,000 times.

  dynamic : 19 ms
  prototype : 20 ms
  dynamic 10 : 71 ms
  prototype 10 methods (obj) : 945 ms
  prototype 10 methods (items) : 872 ms
  prototype 10 methods (items alt) : 846 ms

Scott González

unread,
May 10, 2010, 10:57:10 PM5/10/10
to nod...@googlegroups.com
You shouldn't be generating your constructors inside the loops. That's extra overhead that wouldn't be encountered in a real scenario.

nwhite

unread,
May 10, 2010, 11:04:07 PM5/10/10
to nod...@googlegroups.com


2010/5/10 Scott González <scott.g...@gmail.com>

You shouldn't be generating your constructors inside the loops. That's extra overhead that wouldn't be encountered in a real scenario.


:/ your right.


  dynamic : 20 ms
  prototype : 12 ms
  dynamic 10 : 70 ms
  prototype 10 methods (obj) : 10 ms
  prototype 10 methods (items) : 11 ms
  prototype 10 methods (items alt) : 10 ms

 

On Mon, May 10, 2010 at 10:22 PM, nwhite <change...@gmail.com> wrote:
I ran some benchmarks on my laptop, each test was looped 500,000 times.

  dynamic : 19 ms
  prototype : 20 ms
  dynamic 10 : 71 ms
  prototype 10 methods (obj) : 945 ms
  prototype 10 methods (items) : 872 ms
  prototype 10 methods (items alt) : 846 ms


--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.

--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.

Scott González

unread,
May 10, 2010, 11:08:42 PM5/10/10
to nod...@googlegroups.com
Those numbers seem much more reasonable :-)

Jesse Hallett

unread,
May 12, 2010, 2:25:30 AM5/12/10
to nodejs
I thought I would chime in with some memory tests. I modified
Nathan's code slightly and ran the benchmarks in Google Chrome using
Chrome's task manager to measure the memory increase for each
benchmark. Each benchmark was run in a fresh tab.

== 500,000 iterations, run 1
dynamic: 7,796K to 66,144K, 315 ms
prototype: 7,760K to 44,484K, 110 ms
dynamic 10: 7,796K to 364,440K, 1993 ms
prototype 10 methods (obj): 5,488K to 38,616K, 107 ms
prototype 10 methods (items): 7,404K to 39,952K, 109 ms
prototype 10 methods (items alt): 7,532K to 41,776K, 109 ms

== 500,000 iterations, run 2
dynamic: 5,860K to 62,948K, 256 ms
prototype: 7,792K to 44,480K, 134 ms
dynamic 10: 7,636K to 357,512K, 1986 ms
prototype 10 methods (obj): 7,796K to 40,824K, 104 ms
prototype 10 methods (items): 7,760K to 40,644K, 103 ms
prototype 10 methods (items alt): 7,556K to 40,648K, 104 ms

It looks like the average memory footprint of 500,000 objects using
the prototype patterns is 33,154K and with the dynamic method it is
353,260K.

slightly modified benchmark code: http://gist.github.com/398273

On May 10, 8:08 pm, Scott González <scott.gonza...@gmail.com> wrote:
> Those numbers seem much more reasonable :-)
>
>
>
>
>
> On Mon, May 10, 2010 at 11:04 PM, nwhite <changereal...@gmail.com> wrote:
>
> > 2010/5/10 Scott González <scott.gonza...@gmail.com>
>
> > You shouldn't be generating your constructors inside the loops. That's
> >> extra overhead that wouldn't be encountered in a real scenario.
>
> > :/ your right.
>
> >   dynamic : 20 ms
> >   prototype : 12 ms
> >   dynamic 10 : 70 ms
> >   prototype 10 methods (obj) : 10 ms
> >   prototype 10 methods (items) : 11 ms
> >   prototype 10 methods (items alt) : 10 ms
>
> >http://gist.github.com/396836updated
>
> >> On Mon, May 10, 2010 at 10:22 PM, nwhite <changereal...@gmail.com> wrote:
>
> >>> I ran some benchmarks on my laptop, each test was looped 500,000 times.
>
> >>>   dynamic : 19 ms
> >>>   prototype : 20 ms
> >>>   dynamic 10 : 71 ms
> >>>   prototype 10 methods (obj) : 945 ms
> >>>   prototype 10 methods (items) : 872 ms
> >>>   prototype 10 methods (items alt) : 846 ms
>
> >>> benchmark code :http://gist.github.com/396836
>
> >>>  --
> >>> You received this message because you are subscribed to the Google Groups
> >>> "nodejs" group.
> >>> To post to this group, send email to nod...@googlegroups.com.
> >>> To unsubscribe from this group, send email to
> >>> nodejs+un...@googlegroups.com<nodejs%2Bunsu...@googlegroups.com>
> >>> .
> >>> For more options, visit this group at
> >>>http://groups.google.com/group/nodejs?hl=en.
>
> >>  --
> >> You received this message because you are subscribed to the Google Groups
> >> "nodejs" group.
> >> To post to this group, send email to nod...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> nodejs+un...@googlegroups.com<nodejs%2Bunsu...@googlegroups.com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/nodejs?hl=en.
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "nodejs" group.
> > To post to this group, send email to nod...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nodejs+un...@googlegroups.com<nodejs%2Bunsu...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/nodejs?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/nodejs?hl=en.

Marco Rogers

unread,
May 12, 2010, 3:44:41 PM5/12/10
to nodejs
Nice to see that the different techniques for specifying prototype
methods all perform pretty much the same. If there was significant
difference there it would've destroyed everything I thought I knew
about javascript. I would be interested to see if there was a
difference when the prototype methods were added through the C++ API.

:Marco
Reply all
Reply to author
Forward
0 new messages