unsafe_where_true
unsafe_where_true

Reputation: 6300

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...

Upvotes: 3

Views: 55

Answers (1)

CompSciFly
CompSciFly

Reputation: 170

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.

Upvotes: 1

Related Questions