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

167
Views
In Node.js short arrays handled differently than longer ones

Notice the difference between the way names and names2 is handled. Why am I getting this difference? How can I make names behave like names2?

BTW, I get the same behavior in Firefox.

> names
[
  'Liam',     'Noah',
  'Oliver',   'Elijah',
  'William',  'James',
  'Benjamin', 'Lucas',
  'Olivia',   'Emma',
  'Ava',      'Charlotte',
  'Sophia',   'Amelia',
  'Isabella', 'Mia'
]
> names2
[ 'Noah', 'Oliver', 'Sophia', 'Mia' ]
> names .map((n)=>n+'\n') .reduce((x,y)=>x+y,'') 
'Liam\n' +
  'Noah\n' +
  'Oliver\n' +
  'Elijah\n' +
  'William\n' +
  'James\n' +
  'Benjamin\n' +
  'Lucas\n' +
  'Olivia\n' +
  'Emma\n' +
  'Ava\n' +
  'Charlotte\n' +
  'Sophia\n' +
  'Amelia\n' +
  'Isabella\n' +
  'Mia\n'
> names2 .map((n)=>n+'\n') .reduce((x,y)=>x+y,'') 
'Noah\nOliver\nSophia\nMia\n'
> 
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This is just how long strings are displayed.

For example, try typing this in a node console:

"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"

it will show this output:

'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n' +
  'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
about 4 years ago · Juan Pablo Isaza Report

0

The first part of my question was the important part. The answer I now think is that this behavior is a optimization in some Javascript engines to provide lazy evaluation of reduce for large arrays.

The second question can now be rephrased as "how to force that lazy evaluation to run to completion the way console.log does?" An answer to that would be interesting but I don't need it as I am capable of writing my own reduce function.

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!