Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fs: use faster shallow object cloner
  • Loading branch information
mscdex committed Apr 30, 2017
commit 22345d4727af0e93202da75b4a6d809a7c7d57fe
9 changes: 1 addition & 8 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const constants = process.binding('constants').fs;
const { S_IFMT, S_IFREG, S_IFLNK } = constants;
const util = require('util');
const pathModule = require('path');
const { isUint8Array } = process.binding('util');
const { isUint8Array, shallowClone: copyObject } = process.binding('util');

const binding = process.binding('fs');
const fs = exports;
Expand Down Expand Up @@ -93,13 +93,6 @@ function getOptions(options, defaultOptions) {
return options;
}

function copyObject(source) {
const target = {};
for (const key in source)
target[key] = source[key];
return target;
}

function rethrow() {
// TODO(thefourtheye) Throw error instead of warning in major version > 7
process.emitWarning(
Expand Down
11 changes: 11 additions & 0 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ void WatchdogHasPendingSigint(const FunctionCallbackInfo<Value>& args) {
}


void ShallowClone(const FunctionCallbackInfo<Value>& args) {
if (!args[0]->IsObject()) {
Environment* env = Environment::GetCurrent(args);
return env->ThrowTypeError("obj must be an object");
}
args.GetReturnValue().Set(args[0].As<Object>()->Clone());
Copy link
Copy Markdown
Member

@TimothyGu TimothyGu Apr 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how much we should care about it, but Object::Clone crashes with some special objects, like Map iterators (e.g. new Map().entries()) and possibly proxies.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}


void Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context) {
Expand Down Expand Up @@ -192,6 +201,8 @@ void Initialize(Local<Object> target,
env->SetMethod(target, "startSigintWatchdog", StartSigintWatchdog);
env->SetMethod(target, "stopSigintWatchdog", StopSigintWatchdog);
env->SetMethod(target, "watchdogHasPendingSigint", WatchdogHasPendingSigint);

env->SetMethod(target, "shallowClone", ShallowClone);
}

} // namespace util
Expand Down