Skip to content

Commit a011c32

Browse files
thefourtheyetrevnorris
authored andcommitted
fs: minor refactoring
1. Remove a few unnecessary variables to reduce LoC. 2. Remove redundant `var` definitions of variables in same function. 3. Refactor variables which are defined inside a block and used outside as well. 4. Refactor effect-less code. 5. In `rethrow` function, instead of assigning to `err` and throwing `err` directly throw `backtrace` object. 6. Reassign a defined parameter while also mentioning arguments in the body is one of the optimization killers. So, changing `callback` to `callback_` and declaring a new variable called `callback` in the body. PR-URL: #1870 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
1 parent 8841132 commit a011c32

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

lib/fs.js

+29-28
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ function rethrow() {
4949
if (err) {
5050
backtrace.stack = err.name + ': ' + err.message +
5151
backtrace.stack.substr(backtrace.name.length);
52-
err = backtrace;
53-
throw err;
52+
throw backtrace;
5453
}
5554
};
5655
}
@@ -267,27 +266,25 @@ function ReadFileContext(callback, encoding) {
267266
}
268267

269268
ReadFileContext.prototype.read = function() {
270-
var fd = this.fd;
271-
var size = this.size;
272269
var buffer;
273270
var offset;
274271
var length;
275272

276-
if (size === 0) {
273+
if (this.size === 0) {
277274
buffer = this.buffer = new SlowBuffer(kReadFileBufferLength);
278275
offset = 0;
279276
length = kReadFileBufferLength;
280277
} else {
281278
buffer = this.buffer;
282279
offset = this.pos;
283-
length = size - this.pos;
280+
length = this.size - this.pos;
284281
}
285282

286283
var req = new FSReqWrap();
287284
req.oncomplete = readFileAfterRead;
288285
req.context = this;
289286

290-
binding.read(fd, buffer, offset, length, -1, req);
287+
binding.read(this.fd, buffer, offset, length, -1, req);
291288
};
292289

293290
ReadFileContext.prototype.close = function(err) {
@@ -302,8 +299,7 @@ function readFileAfterOpen(err, fd) {
302299
var context = this.context;
303300

304301
if (err) {
305-
var callback = context.callback;
306-
callback(err);
302+
context.callback(err);
307303
return;
308304
}
309305

@@ -417,7 +413,7 @@ fs.readFileSync = function(path, options) {
417413
if (size === 0) {
418414
buffers = [];
419415
} else {
420-
var threw = true;
416+
threw = true;
421417
try {
422418
buffer = new Buffer(size);
423419
threw = false;
@@ -427,16 +423,18 @@ fs.readFileSync = function(path, options) {
427423
}
428424

429425
var done = false;
426+
var bytesRead;
427+
430428
while (!done) {
431-
var threw = true;
429+
threw = true;
432430
try {
433431
if (size !== 0) {
434-
var bytesRead = fs.readSync(fd, buffer, pos, size - pos);
432+
bytesRead = fs.readSync(fd, buffer, pos, size - pos);
435433
} else {
436434
// the kernel lies about many files.
437435
// Go ahead and try to read some bytes.
438436
buffer = new Buffer(8192);
439-
var bytesRead = fs.readSync(fd, buffer, 0, 8192);
437+
bytesRead = fs.readSync(fd, buffer, 0, 8192);
440438
if (bytesRead) {
441439
buffers.push(buffer.slice(0, bytesRead));
442440
}
@@ -529,8 +527,8 @@ function modeNum(m, def) {
529527
return undefined;
530528
}
531529

532-
fs.open = function(path, flags, mode, callback) {
533-
callback = makeCallback(arguments[arguments.length - 1]);
530+
fs.open = function(path, flags, mode, callback_) {
531+
var callback = makeCallback(arguments[arguments.length - 1]);
534532
mode = modeNum(mode, 0o666);
535533

536534
if (!nullCheck(path, callback)) return;
@@ -585,10 +583,12 @@ fs.read = function(fd, buffer, offset, length, position, callback) {
585583

586584
fs.readSync = function(fd, buffer, offset, length, position) {
587585
var legacy = false;
586+
var encoding;
587+
588588
if (!(buffer instanceof Buffer)) {
589589
// legacy string interface (fd, length, position, encoding, callback)
590590
legacy = true;
591-
var encoding = arguments[3];
591+
encoding = arguments[3];
592592

593593
assertEncoding(encoding);
594594

@@ -623,14 +623,14 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
623623
callback(err, written || 0, buffer);
624624
}
625625

626+
var req = new FSReqWrap();
626627
if (buffer instanceof Buffer) {
627628
// if no position is passed then assume null
628629
if (typeof position === 'function') {
629630
callback = position;
630631
position = null;
631632
}
632633
callback = maybeCallback(callback);
633-
var req = new FSReqWrap();
634634
req.oncomplete = strWrapper;
635635
return binding.writeBuffer(fd, buffer, offset, length, position, req);
636636
}
@@ -647,7 +647,6 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
647647
length = 'utf8';
648648
}
649649
callback = maybeCallback(position);
650-
var req = new FSReqWrap();
651650
req.oncomplete = bufWrapper;
652651
return binding.writeString(fd, buffer, offset, length, req);
653652
};
@@ -721,8 +720,10 @@ fs.truncateSync = function(path, len) {
721720
}
722721
// allow error to be thrown, but still close fd.
723722
var fd = fs.openSync(path, 'r+');
723+
var ret;
724+
724725
try {
725-
var ret = fs.ftruncateSync(fd, len);
726+
ret = fs.ftruncateSync(fd, len);
726727
} finally {
727728
fs.closeSync(fd);
728729
}
@@ -875,7 +876,7 @@ function preprocessSymlinkDestination(path, type, linkPath) {
875876
}
876877
}
877878

878-
fs.symlink = function(destination, path, type_, callback) {
879+
fs.symlink = function(destination, path, type_, callback_) {
879880
var type = (typeof type_ === 'string' ? type_ : null);
880881
var callback = makeCallback(arguments[arguments.length - 1]);
881882

@@ -968,9 +969,9 @@ if (constants.hasOwnProperty('O_SYMLINK')) {
968969

969970
// prefer to return the chmod error, if one occurs,
970971
// but still try to close, and report closing errors if they occur.
971-
var err, err2;
972+
var err, err2, ret;
972973
try {
973-
var ret = fs.fchmodSync(fd, mode);
974+
ret = fs.fchmodSync(fd, mode);
974975
} catch (er) {
975976
err = er;
976977
}
@@ -1088,8 +1089,8 @@ fs.futimesSync = function(fd, atime, mtime) {
10881089
binding.futimes(fd, atime, mtime);
10891090
};
10901091

1091-
function writeAll(fd, buffer, offset, length, position, callback) {
1092-
callback = maybeCallback(arguments[arguments.length - 1]);
1092+
function writeAll(fd, buffer, offset, length, position, callback_) {
1093+
var callback = maybeCallback(arguments[arguments.length - 1]);
10931094

10941095
// write(fd, buffer, offset, length, position, callback)
10951096
fs.write(fd, buffer, offset, length, position, function(writeErr, written) {
@@ -1112,7 +1113,7 @@ function writeAll(fd, buffer, offset, length, position, callback) {
11121113
});
11131114
}
11141115

1115-
fs.writeFile = function(path, data, options, callback) {
1116+
fs.writeFile = function(path, data, options, callback_) {
11161117
var callback = maybeCallback(arguments[arguments.length - 1]);
11171118

11181119
if (!options || typeof options === 'function') {
@@ -1634,8 +1635,8 @@ function ReadStream(path, options) {
16341635
this.flags = options.flags === undefined ? 'r' : options.flags;
16351636
this.mode = options.mode === undefined ? 0o666 : options.mode;
16361637

1637-
this.start = options.start === undefined ? undefined : options.start;
1638-
this.end = options.end === undefined ? undefined : options.end;
1638+
this.start = options.start;
1639+
this.end = options.end;
16391640
this.autoClose = options.autoClose === undefined ? true : options.autoClose;
16401641
this.pos = undefined;
16411642

@@ -1804,7 +1805,7 @@ function WriteStream(path, options) {
18041805
this.flags = options.flags === undefined ? 'w' : options.flags;
18051806
this.mode = options.mode === undefined ? 0o666 : options.mode;
18061807

1807-
this.start = options.start === undefined ? undefined : options.start;
1808+
this.start = options.start;
18081809
this.pos = undefined;
18091810
this.bytesWritten = 0;
18101811

0 commit comments

Comments
 (0)