2

I am comparing the performance between these 2 blocks of code:

Block 1

for(var i = 0; i < 20000; i++) {
  let a = "a random string";
  a.split("");
}

and Block 2

for(var i = 0; i < 20000; i++) {
  let a = new nativeWindow.String("a random string");
  a.split("");
}

According to this page test https://jsben.ch/Nzfb1 the first block is 50 percent faster.

But I still need to use block2 because I don't want the literal string to autobox to the window.String constructor, I'd like it to autobox to the constructor I define.

Is it possible to achieve this ?

Yaron
  • 1,655
  • 6
  • 20
  • 38
  • 1
    the second returns an object. this is always slower than a primitive value. – Nina Scholz Aug 27 '20 at 07:49
  • Just *never* use `new String`. Really never ever. – Bergi Aug 27 '20 at 08:11
  • @Bergi I am using it as ```new nativeWindow.String("my string")``` to avoid using a corrupt extended string function on my window. I understand a string literal is 'auto-boxed' to the corrupt ```String("some string")``` when I call a method on it. Is there another way to make my string literal auto-box to the ```nativeWindow.String``` version ? – Yaron Aug 27 '20 at 08:40
  • 1
    No - why not just prevent corrupting your `String` methods? Alternatively always use `nativeWindow.String.prototype.slice.call("myString", …)` – Bergi Aug 27 '20 at 10:27

1 Answers1

1

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.

Artem Arkhipov
  • 7,025
  • 5
  • 30
  • 52
  • 2
    Not in firefox - while your suggestion is about 7x faster, the first method is still 28x faster – Jaromanda X Aug 27 '20 at 08:05
  • @ArtemArkhipov thanks for your answer. Can you give an example of a case where using new String("string") is useful ? I am using it because I want to get a pure native string object, while the one I have in my current window was extended. (I am using new native.String("native string").toUpperCase() === "NATIVE STRING" rather than "overridden string".toUpperCase() === this function was overridden !" – Yaron Aug 27 '20 at 08:34