Skip to content

fix: Request execution time keeps increasing over time when using Parse.Object.extend #1682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
60 changes: 37 additions & 23 deletions src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -1959,37 +1959,60 @@ class ParseObject {
let parentProto = ParseObject.prototype;
if (this.hasOwnProperty('__super__') && this.__super__) {
parentProto = this.prototype;
} else if (classMap[adjustedClassName]) {
parentProto = classMap[adjustedClassName].prototype;
}
const ParseObjectSubclass = function (attributes, options) {
let ParseObjectSubclass = function (attributes, options) {
this.className = adjustedClassName;
this._objCount = objectCount++;
// Enable legacy initializers
if (typeof this.initialize === 'function') {
this.initialize.apply(this, arguments);
}

if (this._initializers) {
for (const initializer of this._initializers) {
initializer.apply(this, arguments);
}
}

if (attributes && typeof attributes === 'object') {
if (!this.set(attributes || {}, options)) {
throw new Error("Can't create an invalid Parse Object");
}
}
};
ParseObjectSubclass.className = adjustedClassName;
ParseObjectSubclass.__super__ = parentProto;

ParseObjectSubclass.prototype = Object.create(parentProto, {
constructor: {
value: ParseObjectSubclass,
enumerable: false,
writable: true,
configurable: true,
},
});
if (classMap[adjustedClassName]) {
ParseObjectSubclass = classMap[adjustedClassName];
} else {
ParseObjectSubclass.extend = function (name, protoProps, classProps) {
if (typeof name === 'string') {
return ParseObject.extend.call(ParseObjectSubclass, name, protoProps, classProps);
}
return ParseObject.extend.call(ParseObjectSubclass, adjustedClassName, name, protoProps);
};
ParseObjectSubclass.createWithoutData = ParseObject.createWithoutData;
ParseObjectSubclass.className = adjustedClassName;
ParseObjectSubclass.__super__ = parentProto;
ParseObjectSubclass.prototype = Object.create(parentProto, {
constructor: {
value: ParseObjectSubclass,
enumerable: false,
writable: true,
configurable: true,
},
});
}

if (protoProps) {
for (const prop in protoProps) {
if (prop === 'initialize') {
Object.defineProperty(ParseObjectSubclass.prototype, '_initializers', {
value: [...(ParseObjectSubclass.prototype._initializers || []), protoProps[prop]],
enumerable: false,
writable: true,
configurable: true,
});
continue;
}
if (prop !== 'className') {
Object.defineProperty(ParseObjectSubclass.prototype, prop, {
value: protoProps[prop],
Expand All @@ -2013,15 +2036,6 @@ class ParseObject {
}
}
}

ParseObjectSubclass.extend = function (name, protoProps, classProps) {
if (typeof name === 'string') {
return ParseObject.extend.call(ParseObjectSubclass, name, protoProps, classProps);
}
return ParseObject.extend.call(ParseObjectSubclass, adjustedClassName, name, protoProps);
};
ParseObjectSubclass.createWithoutData = ParseObject.createWithoutData;

classMap[adjustedClassName] = ParseObjectSubclass;
return ParseObjectSubclass;
}
Expand Down
18 changes: 18 additions & 0 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3516,6 +3516,24 @@ describe('ParseObject extensions', () => {
ParseObject.enableSingleInstance();
});

it('can extend object', () => {
const startExtend = Date.now();
for (let i = 0; i < 100000; i++) {
// eslint-disable-next-line
const Parent = ParseObject.extend('Parent');
// eslint-disable-next-line
const parent = new Parent();
}
expect(Date.now() - startExtend).toBeLessThan(200);

const startNew = Date.now();
for (let i = 0; i < 100000; i++) {
// eslint-disable-next-line
const parent = new ParseObject('Parent');
}
expect(Date.now() - startNew).toBeLessThan(200);
});

it('can generate ParseObjects with a default className', () => {
const YourObject = ParseObject.extend('YourObject');
const yo = new YourObject();
Expand Down