Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript: recurring strings and performance

I have a Java background.

There, especially in the JDK 1.2 times, it was mandatory to have a frugal string management. So we would use constants everywhere, and minimize the use of strings, especially when used as keys which are repeated all over the code (and, even more especially, in loops).

In javascript though, I rarely see people using constants for strings - it seems rather profuse that strings are used literally all over the code, even for relatively high quality code I have seen.

Is it because javascript doesn't have problems when handling strings? Does it maybe have some intelligent memory management or something? Actually my intuition tells me it doesn't...

like image 944
transient_loop Avatar asked Dec 29 '25 20:12

transient_loop


1 Answers

I'd like to note that you're comparing an old 1998/1999 Java platform to modern day JavaScript. Obviously, Java has gone through a ton of changes since then.

JavaScript's compatibility history with constants probably has a lot to do with your observation.

According to Mozilla, constants in JavaScript weren't compatible with most browsers until quite recently.

In earlier versions of Firefox & Chrome and as of Safari 5.1.7 and Opera 12.00, if you define a variable with const, you can still change its value later. It is not supported in Internet Explorer 6-10, but is included in Internet Explorer 11.

You could define a constant in JavaScript, but the value could actually still be changed, therefore it wasn't a true constant. Scripts being ran on these browsers would break when some of the instructions with constants were used. Therefore, not many developers were even willing to bother with them.

This was rather interesting to research, many people were confused on the topic.

like image 73
CompSciFly Avatar answered Jan 01 '26 09:01

CompSciFly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!