Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions benchmark/fs/read-write-file-params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

const common = require('../common.js');
const fs = require('fs');
const path = require('path');

const filePath = path.join(__dirname, 'read-write-file-params.txt');
const data = Buffer.from('1');
const options = {};
const cb = () => {};

const configs = {
n: [1e3],
methodName: ['readFile', 'writeFile'],
withOptions: ['true', 'false'],
};

const bench = common.createBenchmark(main, configs);

function main(conf) {
const n = +conf.n;
const methodName = conf.methodName;
const withOptions = conf.withOptions;

const method = fs[methodName];

switch (methodName) {
case 'readFile':
switch (withOptions) {
case 'true':
bench.start();
for (let i = 0; i < n; i++) method(filePath, options, cb);
bench.end(n);
break;
case 'false':
bench.start();
for (let i = 0; i < n; i++) method(filePath, cb);
bench.end(n);
break;
}
break;
case 'writeFile':
switch (withOptions) {
case 'true':
bench.start();
for (let i = 0; i < n; i++) method(filePath, data, options, cb);
bench.end(n);
break;
case 'false':
bench.start();
for (let i = 0; i < n; i++) method(filePath, data, cb);
bench.end(n);
break;
}
break;
}
}
1 change: 1 addition & 0 deletions benchmark/fs/read-write-file-params.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
8 changes: 3 additions & 5 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ fs.existsSync = function(path) {
};

fs.readFile = function(path, options, callback) {
callback = maybeCallback(arguments[arguments.length - 1]);
callback = maybeCallback(callback || options);
options = getOptions(options, { flag: 'r' });

if (handleError((path = getPathFromURL(path)), callback))
Expand Down Expand Up @@ -1166,9 +1166,7 @@ fs.futimesSync = function(fd, atime, mtime) {
binding.futimes(fd, atime, mtime);
};

function writeAll(fd, isUserFd, buffer, offset, length, position, callback_) {
var callback = maybeCallback(arguments[arguments.length - 1]);

function writeAll(fd, isUserFd, buffer, offset, length, position, callback) {
// write(fd, buffer, offset, length, position, callback)
fs.write(fd, buffer, offset, length, position, function(writeErr, written) {
if (writeErr) {
Expand Down Expand Up @@ -1199,7 +1197,7 @@ function writeAll(fd, isUserFd, buffer, offset, length, position, callback_) {
}

fs.writeFile = function(path, data, options, callback) {
callback = maybeCallback(arguments[arguments.length - 1]);
callback = maybeCallback(callback || options);
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });
const flag = options.flag || 'w';

Expand Down