Caling new String(val)
returns an instance object of String. It is not a primitive value, that's why it can be noticeably slower.
typeof new String('a random string') // object
In real life it is very rare case when you need to do such things. Usually String
is called without new
in order to simply convert value to string. In that case String('random string')
will just return the same primitive value you passed in.
typeof String('a random string') // string
Try to add the third block to your tests:
for(var i = 0; i < 20000; i++) {
let a = String("a random string"); // no 'new'
a.split("");
}
And you will see that its performance is almost the same as for the simple initialisation like in your first block.
UPD:
According to tests, in some browsers the first block from the question still executes few times faster than one above. It probably happens because String(val)
is a function call, what makes browser to do more actions than during simple initialisation.
Anyway, the main point of this answer is that creating object is slower than simple initialisation and it is quite unusual for String
to be used like a constructor.