From 9e699ff8a9790b4630ebe170ed4881e0fd37dbfa Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Wed, 15 Oct 2014 16:18:19 -0700 Subject: [PATCH] omit non-enumerable properties from cloned objects (#784) Drastically improves performance in some cases (for example, when using the `--explain`/`-X` option). --- lib/jsdoc/util/doop.js | 4 ++-- test/specs/jsdoc/util/doop.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/jsdoc/util/doop.js b/lib/jsdoc/util/doop.js index 4d082ace..48cd9fc7 100644 --- a/lib/jsdoc/util/doop.js +++ b/lib/jsdoc/util/doop.js @@ -1,7 +1,7 @@ /*global Set */ /** - Deep clone a simple object. + Deep clone a simple object. Ignores non-enumerable properties. @private */ 'use strict'; @@ -53,7 +53,7 @@ function doop(o, seen) { } else { clone = Object.create( Object.getPrototypeOf(o) ); - props = Object.getOwnPropertyNames(o); + props = Object.keys(o); for (i = 0, l = props.length; i < l; i++) { descriptor = Object.getOwnPropertyDescriptor(o, props[i]); if (descriptor.value) { diff --git a/test/specs/jsdoc/util/doop.js b/test/specs/jsdoc/util/doop.js index 7ca972e4..ebff9d9f 100644 --- a/test/specs/jsdoc/util/doop.js +++ b/test/specs/jsdoc/util/doop.js @@ -1,4 +1,6 @@ /*global describe, expect, it */ +'use strict'; + describe('jsdoc/util/doop', function() { var doop = require('jsdoc/util/doop'); @@ -77,6 +79,19 @@ describe('jsdoc/util/doop', function() { compareForEquality(inp, out); }); + it('should not clone non-enumerable properties', function() { + var clone; + var obj = { a: 1 }; + + Object.defineProperty(obj, 'foo', { + value: 2 + }); + + clone = doop(obj); + + expect(clone.foo).not.toBeDefined(); + }); + it('should not create a circular reference if an object is seen more than once', function() { var input = { a: {} }; var output;