shawnnyglum
shawnnyglum

Reputation: 221

Javascript: does creating a string with new mean I need to release it later?

If I create a String Object by using say,

var temp = new String('ABCD');

Does this creates a memory space for this String object, like Objects in JavaScript? If yes, can I release this object just by assigning this variable 'temp' to null i.e. temp = null?

Upvotes: 3

Views: 413

Answers (4)

Ruan Mendes
Ruan Mendes

Reputation: 92274

To answer the question, JS is garbage collected, so there's no need to release memory. If you have a really gigantic string that you're only referencing from one place, setting that reference to null will release that string in the garbage collection pass.

Since you asked about new String() specifically, it's worth mentioning that it's generally considered bad because of two problems:

  • The string created can't be compared with the equals operator
  • typeof yields 'object' instead of 'string'

Here's some code you can run to see the behavior

var a = new String('hello'), b = new String('hello');
var strA = String('hello'), strB = String('hello');
var literalA = 'hello', literalB = 'hello';

// Outputs false, because the string is wrapped by an object,
// it's not the primitive
console.log( a == b );
// Proof that it's not the primitive string, outputs 'object'
console.log(typeof a);

// Using String() and the literal yield consistent results
console.log( strA == strB); //outputs true
conole.log( literalA == literalB); // outputs true
console.log( typeof strA); // outputs 'string'
console.log( typeof literalA ); // outputs 'string'

// You can compare literal and string from String(xxxx)
console.log( strA == literalA)

If you're stuck with a string that you don't know how it was created, you can always use valueOf() to compare them. That is

console.log(a.valueOf() == b.valueOf()); // outputs true;

To properly detect that an object is a string, you can check the constructor property.

console.log(a.constructor == String); // outputs true

Just beware that if you're working with frames, popups, iframes, there is a different String constructor for each window object, that is,

// outputs false
console.log( window.frames.frameA.String == window.frames.frameB.String)

Upvotes: 1

Amjad Masad
Amjad Masad

Reputation: 4035

There is no apparent use of creating a string using the constructor (Wrapper Object) unless you intend to check the [[Class]] property, and if your so interested in memory optimization use the primitive type temp = "ABCD"
And yes it takes space in memory, and when you assign null to the variable, and there is no other reference to the string it is collected by the Garbage collecter. But why do want to do that? (null is an object too)
You can use delete temp to delete the variable, hence the string is garbage collected

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691715

Of course, The JavaScript runtime will need to store this String object in memory. Assigning null to the variable might free the memory if the string isn't referenced anymore and if the garbage collector decides to free the memory.

Upvotes: 1

Quentin
Quentin

Reputation: 943480

Does this creates a memory space for this String Object, like Objects in Javascript?

String objects are Objects.

If yes, can I release this object just by assigning this variable 'temp' to null i.e. temp = null ?

Garbage collection works as normal.

Upvotes: 4

Related Questions