Hello V8 team, thanks for making V8 engine which could run JavaScript super 
fast.

Recently, I want to improve the performance of deserialization for Cocos 
Creator game engine.

Cocos Creator uses V8 for its default JavaScript engine and benefited from 
V8's high performance. There're many classes implemented in JS and some 
implemented in C++. And there will be more and more JS code moved to C++ 
side.

I thought about deserializing game assets directly in C++ rather than in JS 
to get better performance since I think C++ code should be faster.

Therefore, I wrote a testcase to test whether we could get benefited from 
C++ code. But unfortunately, I got a really bad result saying that create 
JS object and setting properties in C++ by V8 API is really slower than 
doing that in JS. It's about 50 times slower, in another word, JS execution 
is really fast.

I debugged a little bit and found that the most heavy cost is in 
v8::Object::Set and v8::String::NewFromUtf8.

Part of JS test code:
const testArr = new Array<MySerializationTestClass>(this.loopCount);
        const start = performance.now();

        for (let i = 0; i < this.loopCount; ++i) {
            const test = new MySerializationTestClass();
            testArr[i] = test;

            test['booleanValue'] = false;
            test['nullValue'] = null;
            test['undefinedValue'] = undefined;
            test['numberValue0'] = 100.123;
            test['numberValue1'] = 230.2345;
            test['numberValue2'] = 330.2345;
            test['numberValue3'] = 430.2345;
            test['numberValue4'] = 530.2345;

            test['stringValue0'] = 'hello 0';

            test['stringValue1'] = 'hello 1';

The logic of CPP test code is the same as which in the above JS test code. 
The difference is that we use v8::Object::Set API.

### JIT Enabled

Cpp code is 55 times slower than JS code.

### JIT Disabled

Cpp code is 16 times slower than JS code.


### Xcode Profile


`v8::Maybe<bool> ret = 
_obj.handle(__isolate)->Set(__isolate->GetCurrentContext(), 
nameValue.ToLocalChecked(), value);` and `v8::String::NewFromUtf8` are the 
main hotspots.

### What I guess

I guess running JS code directly could make use of the optimization by V8 
Bytecode and JIT by TurboFan. But invoking V8 C++ API will not get the 
optimization. 


Could you point me out that whether my guess is correct or not? Also, is 
there a better V8 API for setting property?  Thanks.

 

-- 
-- 
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- 
You received this message because you are subscribed to the Google Groups 
"v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-dev/721fa65a-e310-4315-9166-e6beae89ce47n%40googlegroups.com.

Reply via email to