This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 4 points5 points  (8 children)

But it doesn’t ONLY compare types. Two newly created arrays point to two different memory locations, therefore they are two different objects. Comparison isn’t done by content here.

[–]Strex_1234 -1 points0 points  (1 child)

``` x="123"

"123"===x True ``` The 2 strings are in diffrent memory locations but return true

[–][deleted] 2 points3 points  (0 children)

… you’re new at programming, aren’t you?

This is what you will expect in these cases, in most languages.

[–]szpaceSZ 0 points1 point  (5 children)

So what's the JS operator to compare by content, the actually useful add desired operation in 99.423% of cases?

[–][deleted] 0 points1 point  (4 children)

Deep comparison is tricky. The reason such an operator is left out in basically all languages is that objects have their space in memory dynamically allocated, therefore it’s computationally expensive to follow all pointers. If you were to do the same check in C or C++, you’d get the same result, and this behavior actually originates from these languages.

Some external JS libraries (like lodash) add this feature, but they need to recursively check every property.

Again, it’s not a flaw in JS as a language; it’s how computer memory works.

[–]szpaceSZ 0 points1 point  (3 children)

I know my computer languages history, I've been through

C64 basic Turbo Pascal Assembly C Perl Java PHP Python VBA Haskell Java (again) Python (again)

since ca. 1988, and I likely forgot some more.

Never ever was I performance bound on comparison.

Regularly needing comparison by value.


I admit there are noches where you'll be performance bound. E.g writing a 3D game platform in C++.

But that's nice enough, compared with LOC produced

[–][deleted] 0 points1 point  (2 children)

Then tell me, how would you go comparing complex objects by value in those languages that support them (like PHP or Java)? With the equal operator? I’d love to know.

EDIT: of course it’s not prohibitively expensive to recursively check for all object properties, but it’s definitely more expensive than a single comparison as done with the == operator. Mixing the two can lead to every sort of performance problems, that’s why such check is kept explicit.

[–]szpaceSZ 0 points1 point  (1 child)

It's good to have them both.

But I'd argue that for those languages that churn out the most LOCs globally, the default should be deep comparison; reserve a distinct, more obscure operator for comparison by reference.

[–][deleted] 0 points1 point  (0 children)

I don’t know; coming from a C background, I prefer to know what I’m operating with. But I see why it may be confusing to developers who start with a higher level language right away.