Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9237977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:39:11+00:00 2026-06-18T07:39:11+00:00

I was playing around with node.js and I found that this simple program is

  • 0

I was playing around with node.js and I found that this simple program is running incredibly slowly, and I didn’t even wait around to see how long it took after 3 minutes had passed.

var fs = require ('fs')
var s = fs.createWriteStream("test.txt");
for (i = 1; i <= 1000000; i++)
      s.write(i+"\n");
s.end()

I experimented using different values, and found that while 1-112050 takes 3 seconds, 1-112051 takes over a minute. This sudden dropoff is strange. The same program in python, or the equivalent shell script ‘seq 1 112051` runs in a reasonable amount of time (0-2 seconds).

Note that this node.js app runs much faster:

var fs = require('fs')
     , s = []
for (var i = 1; i <= 1000000; i++) s.push(i.toString())
s.push('')
fs.writeFile('UIDs.txt', s.join('\n'), 'utf8')

Can anyone explain to me why node.js behaves this way, and why the dropoff is so sudden?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:39:12+00:00Added an answer on June 18, 2026 at 7:39 am

    It’s a buffer that gets filled up. Each write will return true or false depending on the state of the kernel buffer.

    If you start listen to the return code and use the drain event, it will at least be consistant in speed.

    var fs = require ('fs') 
    
    function runTest(stop) {
      var s = fs.createWriteStream("test.txt");
      var startTime = Date.now();
      var c = 1;
      function doIt() {
        while (++c <= stop) {
          if (!s.write(c+"\n")) {
            s.once('drain', doIt);
            return;
          }
        }
    
        s.end();
        var diffTime = Date.now() - startTime;
        console.log(stop+': took '+diffTime+'ms, per write: '+(diffTime/stop)+'ms')
      }
    
      doIt();
    }
    
    runTest(10000);
    runTest(100000);
    runTest(1000000);
    runTest(10000000);
    runTest(100000000);
    

    Output:

    $ node test.js
    10000: took 717ms, per write: 0.0717ms
    100000: took 5818ms, per write: 0.05818ms
    1000000: took 42902ms, per write: 0.042902ms
    10000000: took 331583ms, per write: 0.0331583ms
    100000000: took 2542195ms, per write: 0.02542195ms
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am playing around with node.js and based on this wonderful tutorial I created
So I've been playing around with Nodes and keep running into this error when
I have been playing around with Node.js for two days now, I am slowly
Playing around with ember, I found that sometimes the model is stored on the
I've been exploring javascript more deeply lately, playing around with a node,redis,socket.io,express simple webapp.
I was playing around with node.js and something strange happens when you run this
I'm playing around with Journey on node and I only realized that the router
I am playing around with node.js and socket.io-client. I am trying to connect to
Playing around with MongoDB and NoRM in .NET. Thing that confused me - there
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.