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

  • Home
  • SEARCH
  • 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 3793154
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:43:56+00:00 2026-05-19T12:43:56+00:00

Does javascript use immutable or mutable strings? Do I need a string builder?

  • 0

Does javascript use immutable or mutable strings? Do I need a “string builder”?

  • 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-05-19T12:43:57+00:00Added an answer on May 19, 2026 at 12:43 pm

    They are immutable. You cannot change a character within a string with something like var myString = "abbdef"; myString[2] = 'c'. The string manipulation methods such as trim, slice return new strings.

    In the same way, if you have two references to the same string, modifying one doesn’t affect the other

    let a = b = "hello";
    a = a + " world";
    // b is not affected
    

    Myth Debunking – String concatenation is NOT slow

    I’ve always heard what Ash mentioned in his answer (that using Array.join is faster for concatenation) so I wanted to test out the different methods of concatenating strings and abstracting the fastest way into a StringBuilder. I wrote some tests to see if this is true (it isn’t!).

    This was what I believed would be the fastest way, avoiding push and using an array to store the strings to then join them in the end.

    class StringBuilderArrayIndex {
      array = [];
      index = 0;
      append(str) {
        this.array[this.index++] = str 
      }
      toString() {
        return this.array.join('')
      }
    }
    

    Some benchmarks

    • Read the test cases in the snippet below
    • Run the snippet
    • Press the benchmark button to run the tests and see results

    I’ve created two types of tests

    • Using Array indexing to avoid Array.push, then using Array.join
    • Straight string concatenation

    For each of those tests, I looped appending a constant value and a random string;

    <script benchmark>
    
      // Number of times to loop through, appending random chars
      const APPEND_COUNT = 1000;
      const STR = 'Hot diggity dizzle';
      
      function generateRandomString() {
        const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        const length = Math.floor(Math.random() * 10) + 1; // Random length between 1 and 10
        let result = '';
    
        for (let i = 0; i < length; i++) {
          const randomIndex = Math.floor(Math.random() * characters.length);
          result += characters.charAt(randomIndex);
        }
        return result;
      }
    
      const randomStrings = Array.from({length: APPEND_COUNT}, generateRandomString);
      
      class StringBuilderStringAppend {
        str = '';
    
        append(str) {
          this.str += str;
        }
    
        toString() {
          return this.str;
        }
      }
      
      class StringBuilderArrayIndex {
        array = [];
        index = 0;
    
        append(str) {
          this.array[this.index] = str;
          this.index++;
        }
    
        toString() {
          return this.array.join('');
        }
      }
    
      // @group Same string 'Hot diggity dizzle'
    
      // @benchmark array push & join 
      {
        const sb = new StringBuilderArrayIndex();
    
        for (let i = 0; i < APPEND_COUNT; i++) {
          sb.append(STR)
        }
        sb.toString();
      }
    
      // @benchmark string concatenation
      {
        const sb = new StringBuilderStringAppend();
    
        for (let i = 0; i < APPEND_COUNT; i++) {
          sb.append(STR)
        }
        sb.toString();
      }
    
      // @group Random strings
    
      // @benchmark array push & join
      {
        const sb = new StringBuilderArrayIndex();
    
        for (let i = 0; i < APPEND_COUNT; i++) {
          sb.append(randomStrings[i])
        }
        sb.toString();
      }
    
      // @benchmark string concatenation
      {
        const sb = new StringBuilderStringAppend();
    
        for (let i = 0; i < APPEND_COUNT; i++) {
          sb.append(randomStrings[i])
        }
        sb.toString();
      }
      
      
    </script>
    <script src="https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js"></script>

    Findings

    Nowadays, all evergreen browsers handle string concatenation better, at least twice as fast.

    i-12600k (added by Alexander Nenashev)

    Chrome/117

    --------------------------------------------------------------------
    Same string 'Hot diggity dizzle'
        string concatenation   1.0x  |  x100000  224  232  254  266  275
        array push & join      3.2x  |  x100000  722  753  757  762  763
    Random strings
        string concatenation   1.0x  |  x100000  261  268  270  273  279
        array push & join      5.4x  |   x10000  142  147  148  155  166
    --------------------------------------------------------------------
    https://github.com/silentmantra/benchmark
    

    Firefox/118

    --------------------------------------------------------------------
    Same string 'Hot diggity dizzle'
        string concatenation   1.0x  |  x100000  304  335  353  358  370
        array push & join      9.5x  |   x10000  289  300  301  306  309
    Random strings
        string concatenation   1.0x  |  x100000  334  337  345  349  377
        array push & join      5.1x  |   x10000  169  176  176  176  180
    --------------------------------------------------------------------
    https://github.com/silentmantra/benchmark
    

    Results below on a 2.4 GHz 8-Core i9 Mac on Oct 2023

    Chrome

    --------------------------------------------------------------------
    Same string 'Hot diggity dizzle'
        string concatenation   1.0x  |  x100000  574  592  594  607  613
        array push & join      2.7x  |   x10000  156  157  159  164  165
    Random strings
        string concatenation   1.0x  |  x100000  657  663  669  675  680
        array push & join      4.3x  |   x10000  283  285  295  298  311
    --------------------------------------------------------------------
    https://github.com/silentmantra/benchmark
    

    Firefox

    --------------------------------------------------------------------
    Same string 'Hot diggity dizzle'
        string concatenation   1.0x  |  x100000  546  648  659  663  677
        array push & join      5.8x  |   x10000  314  320  326  331  335
    Random strings
        string concatenation   1.0x  |  x100000  647  739  764  765  804
        array push & join      2.9x  |   x10000  187  188  199  219  231
    --------------------------------------------------------------------
    https://github.com/silentmantra/benchmark
    

    Brave

    --------------------------------------------------------------------
    Same string 'Hot diggity dizzle'
        string concatenation   1.0x  |  x100000  566  571  572  579  600
        array push & join      2.5x  |   x10000  144  145  159  162  166
    Random strings
        string concatenation   1.0x  |  x100000  649  658  659  663  669
        array push & join      4.4x  |   x10000  285  285  290  292  300
    --------------------------------------------------------------------
    https://github.com/silentmantra/benchmark `
    

    Safari

    --------------------------------------------------------------------
    Same string 'Hot diggity dizzle'
        string concatenation   1.0x  |  x10000   76   77   77   79   82
        array push & join      2.2x  |  x10000  168  168  174  178  186
    Random strings
        string concatenation   1.0x  |  x100000  878  884  889  892  903
        array push & join      2.3x  |   x10000  199  200  202  202  204
    --------------------------------------------------------------------
    https://github.com/silentmantra/benchmark `
    

    Opera

    --------------------------------------------------------------------
    Same string 'Hot diggity dizzle'
        string concatenation   1.0x  |  x100000  577  579  581  584  608
        array push & join      2.7x  |   x10000  157  162  165  166  171
    Random strings
        string concatenation   1.0x  |  x100000  688  694  740  750  781
        array push & join      4.2x  |   x10000  291  315  316  317  379
    --------------------------------------------------------------------
    https://github.com/silentmantra/benchmark
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which algorithm does the JavaScript Array#sort() function use? I understand that it can take
Does javascript coding work better with any particular server language?
A response on SO got me thinking, does JavaScript guarantee a certain endian encoding
JavaScript does funky automatic conversions with objects: var o = {toString: function() {return 40;
Why does this javascript return 108 instead of 2008? it gets the day and
Why does the JavaScript function encodeURIComponent encode spaces to the hex Unicode value %20
In Javascript: How does one find the coordinates (x, y, height, width) of every
Does anybody know a way with JavaScript or CSS to basically grey out a
Does anyone have a library or JavaScript snippet to validate the check digit of
Does a tool exist for dynamically altering running javascript in a browser? For example,

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.