About a year ago I decided to go with v8js to use javascript code within php because it was so much easier to handle.
Today I benchmarked a problem related to the v8 engine and just out of curiosity I ran the same test with Spidermonkey.
A simple A/B Test:
- A = define a JS Function and call it it in a loop
- B = define a JS Function once and call it in a loop
The results for V8 were:
| Runs | 10 | 100 | 1.000 | 10.000 | 100.000 |
|---|---|---|---|---|---|
| A | 0.0048ms | 0.0018ms | 0.0159ms | 0.4257ms | 4.9063ms |
| B | 0.0004ms | 0.0011ms | 0.0072ms | 0.1733ms | 1.8506ms |
It was not really surprising that the one-time-definition was faster.
I tried exact the same with Spidermonkey:
| Runs | 10 | 100 | 1.000 | 10.000 | 100.000 |
|---|---|---|---|---|---|
| A | 0.0016ms | 0.0276ms | 0.2141ms | 1.8415ms | 18.483ms |
| B | 0.0011ms | 0.0039ms | 0.0713ms | 0.8591ms | 8.4125ms |
Huge difference compared to V8!
Here’s the test code:
V8 Test:
0; $i-- ) {
$js->executeString($jsFunc, 'Test.Context');
$js->executeString("myTestFunc();", 'Test.Context');
}
echo "#1: " . (mstime() - $start)." ({$runs} with re-definition)";
unset($js);
$start = mstime();
$js = new V8Js('Test');
$js->executeString($jsFunc, 'Test.Context');
for ( $i = $runs; $i > 0; $i-- ) {
$js->executeString("myTestFunc();", 'Test.Context');
}
echo "#2: " . (mstime() - $start)." ({$runs} without re-definition)";
unset($js);
echo "
";
}
function mstime() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
Spidermonkey Test:
0; $i-- ) {
$js->evaluateScript($jsFunc, 'Test.Context');
$js->evaluateScript("myTestFunc();", 'Test.Context');
}
echo "#1: " . (mstime() - $start)." ({$runs} with re-definition)";
unset($js);
$start = mstime();
$js = new JSContext('Test');
$js->evaluateScript($jsFunc, 'Test.Context');
for ( $i = $runs; $i > 0; $i-- ) {
$js->evaluateScript("myTestFunc();", 'Test.Context');
}
echo "#2: " . (mstime() - $start)." ({$runs} without re-definition)";
unset($js);
echo "
";
}
function mstime() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
