trying to understand performance implications

33 views
Skip to first unread message

JBaron

unread,
Dec 30, 2010, 12:17:15 PM12/30/10
to v8-users
In the process of developing a widget-portal using V8 JavaScript
engine and node.js, I decided to try some small performance benchmarks
to understand the different performance implications of using certain
"patterns". Overall the performance is incredible, V8 is really a
great engine.

One thing however I don't understand is why sometimes accessing object
properties is slower and sometimes it is very fast. For example in the
following code snippet the differences are big (using V8 3.0.3 with
node.js 0.3.2, however there is no node.js related code in example). I
guess some optimization is going on I'm not aware of. But anyone who
can shine a light would be a great help!

#####################################

function loop(y) {
var len = 1000*1000*10;
var object = {};
var start = Date.now();
while (len--) {
object[y] = "someValue";
}
return Date.now()-start;
};

var x = "test101"; // this is fast
var y = "test" + "101"; // this is slower ???

var tries = 10;
while (tries--) {
console.log("Fast:" + loop(x));
console.log("Slow:" + loop(y));
}

####################################

regards, Peter

Florian Schneider

unread,
Jan 1, 2011, 9:27:41 AM1/1/11
to v8-u...@googlegroups.com
Thanks for your observation! On my machine I get the following results:

Fast:103
Slow:1511
Fast:934
Slow:1519
Fast:933
etc.

So far I can see two performance differences with your example: Slow is always slower than Fast, subsequent executions of Fast are slower than the first execution. Here is an attempt to explain why at a high level:

1. Property access with a symbol string literal ("test101") are faster than using a generic key expression. ("test" + "101" is not treated as a symbol currently). Since each property access is optimized for the type of receiver and key seen, it is not a good idea to mix different types of keys at the same access site.

2. The store object[y] = "someValue" adds a property to object on the first execution. This triggers a so-called map transition internally in V8. As a general rule, it is not a good idea to have a store that may add a property inside a loop.

With the following modifications below to your example I get consistently fast performance for the Fast case:

Fast:89
Slow:1471
Fast:74
Slow:1439
Fast:74
Slow:1437


#####################################

function loop2(y) {

       var len = 1000*1000*10;
       var object = {};
       var start = Date.now();
       object[y] = "someValue";

       while (len--) {
               object[y] = "someValue";
       }
       return Date.now()-start;
}; 
function loop(y) {
       var len = 1000*1000*10;
       var object = {};
       var start = Date.now();
       while (len--) {
               object[y] = "someValue";
       }
       return Date.now()-start;
};
var x = "test101"; // this is fast
var y = "test" + "101"; // this is slower ???

var tries = 10;
while (tries--) {
       console.log("Fast:" + loop2(x));
       console.log("Slow:" + loop(y));
}

Hope this helps.

--Florian
 

####################################

regards, Peter

--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users

Stephan Beal

unread,
Jan 1, 2011, 10:06:46 AM1/1/11
to v8-u...@googlegroups.com
On Sat, Jan 1, 2011 at 3:27 PM, Florian Schneider <fschn...@chromium.org> wrote:
receiver and key seen, it is not a good idea to mix different types of keys at the same access site.
...
called map transition internally in V8. As a general rule, it is not a good idea to have a store that may add a property inside a loop.

Jus to clarify: when you say "not a good idea," you mean only for performance reasons, right? There are no correctness issues with doing so, are there?
 
--
----- stephan beal
http://wanderinghorse.net/home/stephan/

Florian Schneider

unread,
Jan 1, 2011, 10:22:41 AM1/1/11
to v8-u...@googlegroups.com
Den 1. jan. 2011 16.06 skrev Stephan Beal <sgb...@googlemail.com>:
On Sat, Jan 1, 2011 at 3:27 PM, Florian Schneider <fschn...@chromium.org> wrote:
receiver and key seen, it is not a good idea to mix different types of keys at the same access site.
...
called map transition internally in V8. As a general rule, it is not a good idea to have a store that may add a property inside a loop.

Jus to clarify: when you say "not a good idea," you mean only for performance reasons, right? There are no correctness issues with doing so, are there?
 

Yes, it works perfectly fine. - Just performance-wise not a good idea.

--Florian
 

--

JBaron

unread,
Jan 1, 2011, 4:29:48 PM1/1/11
to v8-users

Florian,

Thanks for the clear explanation, very helpful!!

Regarding "test" + "101" not being optimized yet as a symbol, that is
something I guess I should have grasped myself :( However the
explanation on the "map transition" and sample code you provided is
something I'm sure I would not have quickly figured out myself.


regards,

Peter
> >       * object[y] = "someValue";*
Reply all
Reply to author
Forward
0 new messages