Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8931465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:07:13+00:00 2026-06-15T09:07:13+00:00

Consider this javascript code: var s = Some string; s = More string; Will

  • 0

Consider this javascript code:

var s = "Some string";
s = "More string";

Will the garbage collector (GC) have work to do after this sort of operation?

(I’m wondering whether I should worry about assigning string literals when trying to minimize GC pauses.)

e: I’m slightly amused that, although I stated explicitly in my question that I needed to minimize GC, everyone assumed I’m wrong about that. If one really must know the particular details: I’ve got a game in javascript — it runs fine in Chrome, but in Firefox has semi-frequent pauses, that seem to be due to GC. (I’ve even checked with the MemChaser extension for Firefox, and the pauses coincide exactly with garbage collection.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:07:14+00:00Added an answer on June 15, 2026 at 9:07 am

    Yes, strings need to be garbage-collected, just like any other type of dynamically allocated object. And yes, this is a valid concern as careless allocation of objects inside busy loops can definitely cause performance issues.

    However, string values are immutable (non-changable), and most modern JavaScript implementations use “string interning”, that is they store only one instance of each unique string value. This means that if you have something like this…

     var s1 = "abc",
         s2 = "abc";
    

    …only one instance of “abc” will be allocated. This only applies to string values, not String objects.

    A couple of things to keep in mind:

    1. Functions like substring, slice, etc. will allocate a new object for each function call (if called with different parameters).

    2. Even though both variable point to the same data in memory, there are still two variables to process when the GC cycle runs. Having too many local variables can also hurt you as each of them will need to be processed by the GC, adding overhead.

    Some further reading on writing high-performance JavaScript:

    • https://developer.mozilla.org/en-US/docs/JavaScript/Memory_Management
    • https://www.scirra.com/blog/76/how-to-write-low-garbage-real-time-javascript
    • http://jonraasch.com/blog/10-javascript-performance-boosting-tips-from-nicholas-zakas
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this (rather pointless) javascript code: function make_closure() { var x = 123, y
Consider this code below: <a href="javascript:void(-1)" id="a1">a1</a> <a href="javascript:void(-1)" id="a2">a2</a> <script type="text/javascript"> var buttons
I am trying to work with arrays in javascript. Consider the following code: var
I have been doing some JavaScript development and I encountered this problem. Consider the
Consider this code: <script type=text/javascript> if ('mySuperProperty' in window) { alert(window['mySuperProperty']); } var mySuperProperty
Consider this javascript code: var bar = function () { alert(A); } var foo
consider this code: var deSaturated = deSaturate(greyscaleCtx.getImageData(0, 0, canvasWidth, canvasHeight)); imageData comes from getImageData
Consider this example Javascript code: a = new Array(); a['a1']='foo'; a['a2']='bar'; b = new
Analyzing the location.hash with this simple javascript code: <script type="text/javascript">alert(location.hash);</script> I have a difficult
Consider the following piece of code: <html><head></head> <body> <script type=text/javascript> var outside_scope = outside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.