Skip to content

Commit

Permalink
use node-v8-clone for performance, fallback to lodash #29
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore authored and phated committed Sep 27, 2016
1 parent ffbbeea commit b43709f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
node_modules
build
*.node
components
components
coverage
20 changes: 12 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
var path = require('path');

var clone;
try {
clone = require('node-v8-clone').clone;
} catch(e) {
clone = require('lodash').clone;
}
var cloneStats = require('clone-stats');
var cloneDeep = require('lodash').cloneDeep;

var cloneBuffer = require('./lib/cloneBuffer');
var isBuffer = require('./lib/isBuffer');
var isStream = require('./lib/isStream');
var isNull = require('./lib/isNull');
var inspectStream = require('./lib/inspectStream');
var cloneBuffer = require('./lib/cloneBuffer');

function File(file) {
if (!file) file = {};
Expand Down Expand Up @@ -54,18 +58,18 @@ File.prototype.clone = function(opt) {
opt.contents = opt.contents !== false;
}

var clone = new File();
var file = new File();

Object.keys(this).forEach(function(key) {
if (key !== '_contents' && key !== 'stat') {
clone[key] = opt.deep === true ? cloneDeep(this[key]) : this[key];
file[key] = opt.deep === true ? clone(this[key], true) : this[key];
}
}, this);

clone.contents = opt.contents && this.isBuffer() ? cloneBuffer(this.contents) : this.contents;
clone.stat = this.stat ? cloneStats(this.stat) : null;
file.contents = opt.contents && this.isBuffer() ? cloneBuffer(this.contents) : this.contents;
file.stat = this.stat ? cloneStats(this.stat) : null;

return clone;
return file;
};

File.prototype.pipe = function(stream, opt) {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"lodash.templatesettings": "^2.4.1",
"event-stream": "^3.1.0"
},
"optionalDependencies": {
"node-v8-clone": "~0.6.2"
},
"scripts": {
"test": "mocha --reporter spec && jshint . --exclude node_modules",
"coveralls": "istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage"
Expand Down
24 changes: 21 additions & 3 deletions test/File.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var File = require('../');
var Stream = require('stream');
var fs = require('fs');
var path = require('path');
Expand All @@ -8,6 +7,26 @@ require('mocha');

describe('File', function() {

describe('using node-v8-clone', function() {
var File = require('../');
testFile(File);
});

describe('using lodash', function() {
delete require.cache[path.join(__dirname, '../index.js')];
var clonePath = path.join(__dirname, '../node_modules/node-v8-clone/lib/clone.js');
var exports = require.cache[clonePath].exports;
// test lodash when node-v8-clone is not found
require.cache[clonePath].exports = undefined;
after(function() {
require.cache[clonePath].exports = exports;
});
var File = require('../');
testFile(File);
});
});

function testFile(File) {
describe('constructor()', function() {
it('should default cwd to process.cwd', function(done) {
var file = new File();
Expand Down Expand Up @@ -703,5 +722,4 @@ describe('File', function() {
}).should.throw('path should be string');
});
});

});
}

0 comments on commit b43709f

Please sign in to comment.