3

I want to know how is the string length of a string calculated in js. Is is a function call or a class data member. I want to know what happens when we execute the following code :

a = 'this is a string';
console.log(a.length); // what actually happens at this point?

Also if a do this :

a += ' added something';
console.log(a.length); // at what point is the new length calculated 
//and/or updated for the object 'a';

And at last, do I need to store the string length in a temp variable while using a loop over the string or can I directly use the following (which one is faster/processor efficient) :

for(var i=0;i<a.length;i++){
  // doing anything here
}

Summing up my question, I want to know the processing behind String.length and which practice is better while looping over strings?

3 Answers 3

7

A string is immutable in JavaScript.

a += "somestring" doesn't change the length of a string but makes a new string.

This means there is no "new length", but the length is just part of the definition of the string (more precisely it is stored in the same structure in implementations).

Regarding

for(i=0;i<a.length;i++){ // did you forget the 'var' keyword ?

a not so uncommon practice (if you don't change a) was to optimize it as

for (var i=0, l=a.length; i<l; i++)

in order to avoid the reading of the length but if you compare the performances with modern engines, you'll see this doesn't make the code any faster now.

What you must remember : querying the length of a string is fast because there is no computation. What's a little less fast is building strings (for example with concatenation).

Sign up to request clarification or add additional context in comments.

9 Comments

You mean to say that a variable storing the string's length is created at the time of initialization of the string?
It's not a different variable. The string object internally store its length, like Pascal strings. But you shouldn't worry about how it is stored, the important fact is that there is no computation when you request the string's length.
Keep in mind that in the original question the length is calculated each time you go through the loop. So something like for(i=0;i<a.length;i++) { a += "z"; } will result in an endless loop.
It's read, not calculated. There's no iteration contrary to a language like C.
@vijayant Please see my answer. I think that depends - if you create a string object by var s_obj = new String(s_prim);, then the property is stored at initialization. But if you create a string as a string literal like var s_prim = 'foo';, when you call length, the literal will be wrapped to a string object and call the corresponding method to get its length.
|
5

Strings are a primitive type. At least that's what the documentation says. But we can access the length of the string as if we are accessing the property of an object(with the dot notation). Which indicates it's an object, Right?

Turns out, whenever we make a call from the string primitive to some property using the dot notation (for example, say length), the Js engine will take this primitive string and wrap it into an equivalent wrapper object, which is a String object. And then, the .length on that String object returns the length.

Interesting thing to note here is, that when we do something like this, our string still stays the same primitive string during all of this. And a temporary object is created to make our string operation work. Once the required property is fetched, this temporary object is deleted from the memory.

Hope this gives some high level understanding.

1 Comment

Thanks for adding this. This temporary objectifying of the string primitive type is new on this thread.
2

I'm answering your first question.

I'm also curious about this puzzle so I did some search myself, ended up finding -

Based on String documentation from Mozilla:

String literals (denoted by double or single quotes) and strings returned from String calls in a non-constructor context (i.e., without using the new keyword) are primitive strings. JavaScript automatically converts primitives to String objects, so that it's possible to use String object methods for primitive strings. In contexts where a method is to be invoked on a primitive string or a property lookup occurs, JavaScript will automatically wrap the string primitive and call the method or perform the property lookup.

So as I understand, when you use somestring.length, the primitive string will first be wrapped as a String object, and then since the object has its property length, so it's just a internal method call to access and return.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.