Skip to content

readFile in promises very slow #151

Open
@xJeneKx

Description

@xJeneKx

Version

v21.7.1

Platform

Darwin evgeniis.local 23.4.0 Darwin Kernel Version 23.4.0: Wed Feb 21 21:44:43 PST 2024; root:xnu-10063.101.15~2/RELEASE_ARM64_T6000 arm64

Subsystem

No response

What steps will reproduce the bug?

slow

const fs = require('fs/promises');

const start = Date.now();
let count = 0;
for (let i = 0; i < 10000; i++) {
	fs.readFile("./text.txt", { encoding: 'utf-8' })
		.then((data) => {
			if (data !== "Hello, world") throw 1;
			count++
			if (count === 10000) {
				console.log('time: ', Date.now() - start);
			}
		})
		.catch((err) => {
			throw 1
		})
}

fast

const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);

const start = Date.now();
let count = 0;
for (let i = 0; i < 10000; i++) {
	readFile("./text.txt", { encoding: 'utf-8' })
		.then((data) => {
			if (data !== "Hello, world") throw 1;
			count++
			if (count === 10000) {
				console.log('time: ', Date.now() - start);
			}
		})
		.catch((err) => {
			throw 1
		})
}

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior? Why is that the expected behavior?

No response

What do you see instead?

The promise version is 2 times slower. My tests showed 200ms vs 100ms

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions