Skip to content
This repository was archived by the owner on Mar 11, 2021. It is now read-only.

Commit da2f8a7

Browse files
author
bencompton
committed
Rewrite process props loops to optimize performance a bit
1 parent 4f1e9fb commit da2f8a7

1 file changed

Lines changed: 25 additions & 27 deletions

File tree

src/utils/reactify-vue/react-element-creation/PropsProcessor.ts

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ const hasOwn = {}.hasOwnProperty;
1818

1919
const classNames = (...args: any[]) => {
2020
const classes = [];
21-
let length = args.length;
2221

23-
while (length--) {
24-
let arg = args[length];
22+
for (let i = 0, length = args.length; i < length; i++) {
23+
let arg = args[i];
2524

2625
if (!arg) continue;
2726

@@ -32,13 +31,8 @@ const classNames = (...args: any[]) => {
3231
} else if (Array.isArray(arg)) {
3332
classes.push(classNames.apply(null, arg));
3433
} else if (argType === 'object') {
35-
const keys = Object.keys(arg);
36-
let length = keys.length;
37-
38-
while (length--) {
39-
let key = keys[length];
40-
41-
if (hasOwn.call(arg, key) && arg[key]) {
34+
for (let key in arg) {
35+
if (arg[key]) {
4236
classes.push(key);
4337
}
4438
}
@@ -89,7 +83,7 @@ const attributeMap = {
8983

9084
const handleRefs = (element: HTMLElement, vueComponent: IVueComponent, events: {[eventName: string]: Function}, props) => {
9185
if (events) {
92-
Object.keys(events).forEach(eventName => {
86+
for (let eventName in events) {
9387
if (element && element.addEventListener && !((element as any).vueListeners && (element as any).vueListeners[eventName])) {
9488
element.addEventListener(eventName, (...args: any[]) => {
9589
const eventHandler = events[eventName];
@@ -99,7 +93,7 @@ const handleRefs = (element: HTMLElement, vueComponent: IVueComponent, events: {
9993
(element as any).vueListeners = (element as any).vueListeners || {};
10094
(element as any).vueListeners[eventName] = true;
10195
}
102-
});
96+
}
10397
}
10498

10599
if (props['data-vue-ref']) {
@@ -131,10 +125,15 @@ const renameStyleProperties = (stylesObject) => {
131125
return stylesObject;
132126
}
133127

128+
let totalTime = 0;
129+
let timeout;
130+
134131
export class PropsProcessor {
135132
private cachedPropKebabCase: {[camelCasedProp: string]: string};
136133

137134
public getProps(args, children, componentOrComponentName, resolvedComponent, vueComponentInstance) {
135+
const startTime = new Date()
136+
138137
this.addCurrentComponentAsParentOfChildren(children, vueComponentInstance);
139138

140139
const props = {};
@@ -148,6 +147,14 @@ export class PropsProcessor {
148147
this.getInnerHTML(args, props);
149148
this.handleEvents(resolvedComponent, args.on, vueComponentInstance, props)
150149
this.handleRef(args.ref, vueComponentInstance, props);
150+
151+
totalTime += new Date().getTime() - startTime.getTime();
152+
153+
clearTimeout(timeout);
154+
155+
timeout = window.setTimeout(() => {
156+
window.alert(totalTime);
157+
}, 2000)
151158

152159
return props;
153160
}
@@ -191,12 +198,12 @@ export class PropsProcessor {
191198

192199
} else {
193200
if (eventHandlers) {
194-
Object.keys(eventHandlers).forEach(eventName => {
201+
for (let eventName in eventHandlers) {
195202
const camelCasedEventName = `${camelCase('on-' + eventName)}`;
196203
props[camelCasedEventName] = (...eventArgs: any[]) => {
197204
eventHandlers[eventName].apply(parentVueComponentInstance, eventArgs);
198205
};
199-
});
206+
}
200207
}
201208
}
202209
}
@@ -212,11 +219,7 @@ export class PropsProcessor {
212219

213220
private getPropsFromArgs(args, props) {
214221
if (args.props) {
215-
const keys = Object.keys(args.props);
216-
let length = keys.length;
217-
218-
while (length--) {
219-
let prop = keys[length];
222+
for (let prop in args.props) {
220223
props[camelCase(prop)] = args.props[prop];
221224
}
222225
}
@@ -230,12 +233,7 @@ export class PropsProcessor {
230233

231234
private convertAttrsToProps(args, componentOrComponentName, resolvedComponent, props) {
232235
if (args.attrs) {
233-
const keys = Object.keys(args.attrs);
234-
let length = keys.length;
235-
236-
while (length--) {
237-
let attr = keys[length];
238-
236+
for (let attr in args.attrs) {
239237
attr = renameAttribute(componentOrComponentName, attr);
240238

241239
const resolvedVueComponent = resolvedComponent.vueComponent;
@@ -273,8 +271,8 @@ export class PropsProcessor {
273271
let length = children && children.length;
274272

275273
if (children && length && Array.isArray(children)) {
276-
while (length--) {
277-
let child = children[length];
274+
for (let i = 0, length = children.length; i < length; i++) {
275+
let child = children[i];
278276

279277
if (child && child.tag && child.tag.indexOf('f7-') !== -1) {
280278
child.props = {...child.props, parentVueComponent: vueComponent };

0 commit comments

Comments
 (0)