Live data from Hacker News

Why Const Doesn't Make C Code Faster

theartofmachinery.com

11–20 of 197 posts

Re: Why Const Doesn't Make C Code Faster

#11
post #8

I've worked with JavaScript weenies who are in love with the const declaration. They don't have an answer, though, when I ask, "Shouldn't the compiler be able to notice that a variable isn't changed in it's lifetime?" Most of the const values are just local and never leave the function but the weenies continue to stomp their feet when I use "var". The compilers are smart. Let them do their thing.

const doesn't actually stop data from changing, just reassignment.

  const user = {id: 1, status: 'active'};
  user.status = 'disabled';
is valid javascript

Re: Why Const Doesn't Make C Code Faster

#12
post #8

I've worked with JavaScript weenies who are in love with the const declaration. They don't have an answer, though, when I ask, "Shouldn't the compiler be able to notice that a variable isn't changed in it's lifetime?" Most of the const values are just local and never leave the function but the weenies continue to stomp their feet when I use "var". The compilers are smart. Let them do their thing.

your question only makes sense if they are using const for performance reasons... which I don't think anyone does. No real reason for using var anymore over let in production code. I'll still use var in the console for testing stuff.

Re: Why Const Doesn't Make C Code Faster

#13
post #8

I've worked with JavaScript weenies who are in love with the const declaration. They don't have an answer, though, when I ask, "Shouldn't the compiler be able to notice that a variable isn't changed in it's lifetime?" Most of the const values are just local and never leave the function but the weenies continue to stomp their feet when I use "var". The compilers are smart. Let them do their thing.

Const is primarily for documentation to indicate that the assignment happens only once and won't be redefined. Using 'let' or 'var' indicates to the reader that the variables value will be reassigned in the function.

It's not about the compiler.

Re: Why Const Doesn't Make C Code Faster

#14

I have done a lot of profiling work and I have observed similar things. One thing is const, another is virtual functions. A lot of people think they add overhead and avoid them for performance reasons but my profiling almost never showed them as a problem. Same for const and inline. it’s really hard to predict what the optimizer will do. Obviously there are stupid things that can be avoided from the start but in gene…

Inlining especially is call-site dependent. Little of the benefit of inlining comes from eliminating the call overhead on a modern processor. But quite often, functions are called with one or more parameters constant. So that enables a bunch of constant folding for that call site -- the compiler is good at noticing that if(1<7) is always going to be true, and dropping out the whole else branch, which can enable code motion and more common subexpressions... so inlining is often a win but often people misunderstand exactly why.

Re: Why Const Doesn't Make C Code Faster

#15
post #8

I've worked with JavaScript weenies who are in love with the const declaration. They don't have an answer, though, when I ask, "Shouldn't the compiler be able to notice that a variable isn't changed in it's lifetime?" Most of the const values are just local and never leave the function but the weenies continue to stomp their feet when I use "var". The compilers are smart. Let them do their thing.

Actually no, compilers can't assume that `foo` is const.

    var foo = 0;
    eval("foo = 1");
With that said, virtually nobody uses `const` for performance reasons, but for developer readability and avoiding mistakes.

Re: Why Const Doesn't Make C Code Faster

#16
post #8

I've worked with JavaScript weenies who are in love with the const declaration. They don't have an answer, though, when I ask, "Shouldn't the compiler be able to notice that a variable isn't changed in it's lifetime?" Most of the const values are just local and never leave the function but the weenies continue to stomp their feet when I use "var". The compilers are smart. Let them do their thing.

Comparing "const" and "var" is incorrect; the former is block-scoped (like "let"), while the latter is function-scoped.

More to the point, "const" in JS is a maintainability tool to prevent accidental reassignment. I don't think people use it for performance reasons, nor should they.

Re: Why Const Doesn't Make C Code Faster

#17
post #8

I've worked with JavaScript weenies who are in love with the const declaration. They don't have an answer, though, when I ask, "Shouldn't the compiler be able to notice that a variable isn't changed in it's lifetime?" Most of the const values are just local and never leave the function but the weenies continue to stomp their feet when I use "var". The compilers are smart. Let them do their thing.

[deleted]

Re: Why Const Doesn't Make C Code Faster

#18
Unfortunately in C++, the compiler is not allowed to assume that if it passes a const reference to a function, that function will not change the object. The function is allowed to cast away const and modify the object. I'm not happy that they did it that way; I would have preferred it if cast-away-const were more restricted (for example, allowed when calling a child function that takes a char* but doesn't modify the pointed-to C string), with the idea being that if a function has only a pointer to const or a reference for a const object, it has read permission on the object and lacks write permission. But that isn't how the language works.

Re: Why Const Doesn't Make C Code Faster

#20
post #8

I've worked with JavaScript weenies who are in love with the const declaration. They don't have an answer, though, when I ask, "Shouldn't the compiler be able to notice that a variable isn't changed in it's lifetime?" Most of the const values are just local and never leave the function but the weenies continue to stomp their feet when I use "var". The compilers are smart. Let them do their thing.

For JavaScript const is more for letting the developer know intent. If I declare a local variable within a function with const, I am saying this variable should not be reassigned. If I use let I am saying that the variable can be reassigned.

It's nice when looking at someone else's code and being able to tell if they intended to have a variable be reassignable or not.

Maybe you've talked to JavaScript devs who are just bad at explaining their reasoning or maybe they just strictly adhere to linters which encourage the use of const and let and throw linting errors when they come across a var.

Either way, I'd recommend you reevaluate your language and refrain from calling other dev's weenies just for having a different opinion than yours and maybe not being able to properly explain their stance. Using language that borders bullying is not helpful, it's no better than calling someone a snowflake or other degrading term.

Post reply on HN