From 0b7e9c394a798baf4625214db1a08df8a61c2fc6 Mon Sep 17 00:00:00 2001 From: Damian Krzeminski Date: Mon, 7 Aug 2017 08:52:45 -0700 Subject: [PATCH] optimize getter/setter creation for StructArray elements (#5108) --- src/util/struct_array.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/util/struct_array.js b/src/util/struct_array.js index a900cb2af11..452bbec2686 100644 --- a/src/util/struct_array.js +++ b/src/util/struct_array.js @@ -316,14 +316,18 @@ function createStructArrayType(options: StructArrayTypeParameters): Class 1) { + name += c; + } if (name in StructType.prototype) { throw new Error(`${name} is a reserved name and cannot be used as a member name.`); } - Object.defineProperty(StructType.prototype, name, { - get: createGetter(member, c), - set: createSetter(member, c) - }); + Object.defineProperty( + StructType.prototype, + name, + createAccessors(member, c) + ); } } @@ -400,10 +404,10 @@ function createMemberComponentString(member, component) { return `this._structArray.${getArrayViewName(member.type)}[${index}]`; } -function createGetter(member, c) { - return new Function(`return ${createMemberComponentString(member, c)};`); -} - -function createSetter(member, c) { - return new Function('x', `${createMemberComponentString(member, c)} = x;`); +function createAccessors(member, c) { + const code = createMemberComponentString(member, c); + return { + get: new Function(`return ${code};`), + set: new Function('x', `${code} = x;`) + }; }