Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
Are const values re-evaluated in maps and filters

Does JavaScript treat:

array.filter(x => {
  const test = [1, 2, 3]
  return test.includes(x);
});

the same as:

const test = [1, 2, 3]
array.filter(x => {
  return test.includes(x);
});

or will the const cause extra work on each evaluation in the former?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

With the first, you're declaring an array only once.

With the second, you're declaring an array every time. Memory has to be allocated for each array and then garbage collected.

The first is more efficient.

Note that const does not mean the value is constant (except with primitives), const means that the variable reference can't change (for example, you can call .push on test on the inside array, even with const, but you can't reassign test)

However it is possible that your engine could optimize for this

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!