News for hackers. Stuff that matters.
Re:Using const/let instead of var can make JavaScript code r... by cosmosgenius on Wednesday October 21, 2020 @12:53AM (#24844834)
Not sure if its still valid, but i remember doing a performance testing for "hello world"*10mil in nodejs and golang around 4-5 years back. What i noticed was "var" and "const" gave comparable results to golang, but "let" was around 100x slower for no reason. It was just a single variable declaration with "print" statement called multiple times.
Re:Using const/let instead of var can make JavaScript code r... by judge2020 on Wednesday October 21, 2020 @01:22AM (#24844971)
If it was written like

  while true {
    let hello = "world";
  }
The scope would keep getting freed then re-allocated, which will be slower than simply continuously setting an existing variable's memory location over and over again.
Re:Using const/let instead of var can make JavaScript code r... by charliesome on Wednesday October 21, 2020 @02:00AM (#24845143)
While the creation and destruction of a scope every loop iteration is the correct mental model for what this code does, there's no reason that implementations must actually perform any allocations - especially for an example like this where it is trivially determinable that no bindings escape the immediate scope.
Re:Using const/let instead of var can make JavaScript code r... by chrismorgan on Wednesday October 21, 2020 @02:24AM (#24845245)
That would apply to a pure interpreter, but all JS runtimes will do at least some optimisations, even on their baseline profile.
Re:Using const/let instead of var can make JavaScript code r... by ihuman on Wednesday October 21, 2020 @01:37AM (#24845042)
If that was the reason, then shouldn't "const" be as slow as "let"?
Re:Using const/let instead of var can make JavaScript code r... by kjksf on Wednesday October 21, 2020 @02:08AM (#24845181)
It should be valid to move `const` outside of the loop, since by definition it cannot be changed.

`let` can be changed so cannot be optimized out this way (at least not without doing more complex analysis and proving that the variable is not being changed).

Re:Using const/let instead of var can make JavaScript code r... by cosmosgenius on Wednesday October 21, 2020 @07:39PM (#24853499)
let wasn't inside the loop, which is what made it weird.
Re:Using const/let instead of var can make JavaScript code r... by akmittal on Wednesday October 21, 2020 @01:53AM (#24845120)
node const.js 6.58s user 1.25s system 60% cpu 13.007 total

node let.js 7.00s user 1.35s system 51% cpu 16.272 total

node var.js 7.21s user 1.22s system 55% cpu 15.183 total

go run main.go 0.80s user 0.94s system 17% cpu 10.064 total

Re:Using const/let instead of var can make JavaScript code r... by yetanotherme on Wednesday October 21, 2020 @02:50AM (#24845356)
you're not measuring what you think you are