Results will vary depends on javascript engine used. On chrome one I'm getting the same results as Afshin.
So why actually one is slower than another? It's because on toString there will be one more call to C functions inside V8. You can try next to see that by yourself:
- Open empty tab in chrome(to avoid any side effects from already opened pages)
- Open Developers Tools
- Open Profiles tab and start new profile
- Go to Console tab and insert
script1 script, press enter.
- Go to Profile again and stop profiling
Repeat the same with script2
script 1: var boo = 123123; var foo = boo + "";
script 2: var boo = 123123; var foo = boo.toString();
In my case first will result in next stacktrace:
InjectedScript.evaluate
InjectedScript._evaluateAndWrap
while second one:
InjectedScript.evaluate
InjectedScript._evaluateAndWrap
InjectedScript._evaluateOn
evaluate
I think it's more has to do with engine internals than official js spec and probably could be optimized to use the same code path.
boo + ""! Do you think it's not human readable?