Suggestion - String interpolation (Template literals)



  • Probably a minor thing, however, it will save memory and give a performance increase

    When the compiler comes across a string that has been +'d together, for every +, it creates a new string in memory then waits for the old string to be garbage collected

    Example

    let user = 'svnty';
    let date = Date.now();
    let str = 'hello' + user + ' the date is ' + date;
    

    It looks as if we have put two variables into a string, but the compiler has created a new string every time it comes across a + sign
    So for the str variable there has been 4 strings created in memory, only one of which is being used and the others just taking resources until out of scope

    Solution
    Use template literals with the back tick (or grave accent) `

    Example

    let user = 'svnty';
    let date = Date.now();
    let str = `hello ${user} the date is ${date}`;
    

  • Global Moderator

    String concatenation is highly optimized in JS engines, so the benefits of template literals for performance are small. Using template literals on the client side also makes you incompatible with older browsers.

    Also you didn't really relate your advice to NodeBB.


Log in to reply
 

Suggested Topics

  • 2
  • 4
  • 8
  • 5
  • 3
| |