Skip to content

Commit 22345d4

Browse files
committed
fs: use faster shallow object cloner
1 parent 87592ab commit 22345d4

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

lib/fs.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const constants = process.binding('constants').fs;
2828
const { S_IFMT, S_IFREG, S_IFLNK } = constants;
2929
const util = require('util');
3030
const pathModule = require('path');
31-
const { isUint8Array } = process.binding('util');
31+
const { isUint8Array, shallowClone: copyObject } = process.binding('util');
3232

3333
const binding = process.binding('fs');
3434
const fs = exports;
@@ -93,13 +93,6 @@ function getOptions(options, defaultOptions) {
9393
return options;
9494
}
9595

96-
function copyObject(source) {
97-
const target = {};
98-
for (const key in source)
99-
target[key] = source[key];
100-
return target;
101-
}
102-
10396
function rethrow() {
10497
// TODO(thefourtheye) Throw error instead of warning in major version > 7
10598
process.emitWarning(

src/node_util.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ void WatchdogHasPendingSigint(const FunctionCallbackInfo<Value>& args) {
147147
}
148148

149149

150+
void ShallowClone(const FunctionCallbackInfo<Value>& args) {
151+
if (!args[0]->IsObject()) {
152+
Environment* env = Environment::GetCurrent(args);
153+
return env->ThrowTypeError("obj must be an object");
154+
}
155+
args.GetReturnValue().Set(args[0].As<Object>()->Clone());
156+
}
157+
158+
150159
void Initialize(Local<Object> target,
151160
Local<Value> unused,
152161
Local<Context> context) {
@@ -192,6 +201,8 @@ void Initialize(Local<Object> target,
192201
env->SetMethod(target, "startSigintWatchdog", StartSigintWatchdog);
193202
env->SetMethod(target, "stopSigintWatchdog", StopSigintWatchdog);
194203
env->SetMethod(target, "watchdogHasPendingSigint", WatchdogHasPendingSigint);
204+
205+
env->SetMethod(target, "shallowClone", ShallowClone);
195206
}
196207

197208
} // namespace util

0 commit comments

Comments
 (0)