Skip to content
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

Replace instances of Object.create(null) with new EmptyObject(). #3646

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ correctly will need a shim for Object.create.
* Refactor JSON serializer to use _getMappedKey
* Don't normalize the key if not present in the hash
* Add serializeIntoHash to the JSON Serializer
* prefer Object.create(null) for typeMap backing stores. Cache misses are faster, and won’t collide with prototype pollution
* prefer new EmptyObject() for typeMap backing stores. Cache misses are faster, and won’t collide with prototype pollution
* since the recordArrayManager already maintains the uniq index, we can use that to simply push the record onto the record array without needing the safety of addRecords dupe guard. This prevents another O(n) operation
* the string splitting in transitionTo is wasteful and for large payloads can be surprisingly costly. As they never actually change, we should simply cache and forget about them.
* Coalesce find requests, add support for preloading data
Expand Down
3 changes: 2 additions & 1 deletion packages/ember-data/lib/adapters/rest-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import {
MapWithDefault
} from "ember-data/system/map";
import EmptyObject from "ember-data/system/empty-object";
var get = Ember.get;

import BuildURLMixin from "ember-data/adapters/build-url-mixin";
Expand Down Expand Up @@ -926,7 +927,7 @@ var RESTAdapter = Adapter.extend(BuildURLMixin, {
});

function parseResponseHeaders(headerStr) {
var headers = Object.create(null);
var headers = new EmptyObject();
if (!headerStr) { return headers; }

var headerPairs = headerStr.split('\u000d\u000a');
Expand Down
3 changes: 2 additions & 1 deletion packages/ember-data/lib/system/clone-null.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import EmptyObject from "ember-data/system/empty-object";
export default function cloneNull(source) {
var clone = Object.create(null);
var clone = new EmptyObject();
for (var key in source) {
clone[key] = source[key];
}
Expand Down
27 changes: 14 additions & 13 deletions packages/ember-data/lib/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import merge from "ember-data/system/merge";
import RootState from "ember-data/system/model/states";
import Relationships from "ember-data/system/relationships/state/create";
import Snapshot from "ember-data/system/snapshot";
import EmptyObject from "ember-data/system/empty-object";

var Promise = Ember.RSVP.Promise;
var get = Ember.get;
var set = Ember.set;

var _extractPivotNameCache = Object.create(null);
var _splitOnDotCache = Object.create(null);
var _extractPivotNameCache = new EmptyObject();
var _splitOnDotCache = new EmptyObject();

function splitOnDot(name) {
return _splitOnDotCache[name] || (
Expand Down Expand Up @@ -48,13 +49,13 @@ export default function InternalModel(type, id, store, container, data) {
this.id = id;
this.store = store;
this.container = container;
this._data = data || Object.create(null);
this._data = data || new EmptyObject();
this.modelName = type.modelName;
this.dataHasInitialized = false;
//Look into making this lazy
this._deferredTriggers = [];
this._attributes = Object.create(null);
this._inFlightAttributes = Object.create(null);
this._attributes = new EmptyObject();
this._inFlightAttributes = new EmptyObject();
this._relationships = new Relationships(this);
this.currentState = RootState.empty;
this.isReloading = false;
Expand Down Expand Up @@ -87,7 +88,7 @@ export default function InternalModel(type, id, store, container, data) {
would have a implicit post relationship in order to be do things like remove ourselves from the post
when we are deleted
*/
this._implicitRelationships = Object.create(null);
this._implicitRelationships = new EmptyObject();
}

InternalModel.prototype = {
Expand Down Expand Up @@ -263,7 +264,7 @@ InternalModel.prototype = {

flushChangedAttributes: function() {
this._inFlightAttributes = this._attributes;
this._attributes = Object.create(null);
this._attributes = new EmptyObject();
},

/**
Expand Down Expand Up @@ -326,10 +327,10 @@ InternalModel.prototype = {
rollbackAttributes: function() {
var dirtyKeys = Object.keys(this._attributes);

this._attributes = Object.create(null);
this._attributes = new EmptyObject();

if (get(this, 'isError')) {
this._inFlightAttributes = Object.create(null);
this._inFlightAttributes = new EmptyObject();
this.didCleanError();
}

Expand All @@ -346,7 +347,7 @@ InternalModel.prototype = {
}

if (this.isValid()) {
this._inFlightAttributes = Object.create(null);
this._inFlightAttributes = new EmptyObject();
}

this.send('rolledBack');
Expand Down Expand Up @@ -585,7 +586,7 @@ InternalModel.prototype = {
merge(this._data, data);
}

this._inFlightAttributes = Object.create(null);
this._inFlightAttributes = new EmptyObject();

this.send('didCommit');
this.updateRecordArraysLater();
Expand Down Expand Up @@ -656,7 +657,7 @@ InternalModel.prototype = {
this._attributes[keys[i]] = this._inFlightAttributes[keys[i]];
}
}
this._inFlightAttributes = Object.create(null);
this._inFlightAttributes = new EmptyObject();
},

/**
Expand Down Expand Up @@ -708,7 +709,7 @@ InternalModel.prototype = {
var keys = Object.keys(updates);
var length = keys.length;

original = merge(Object.create(null), this._data);
original = merge(new EmptyObject(), this._data);
original = merge(original, this._inFlightAttributes);

for (i = 0; i < length; i++) {
Expand Down
3 changes: 2 additions & 1 deletion packages/ember-data/lib/system/model/model.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PromiseObject } from "ember-data/system/promise-proxies";
import Errors from "ember-data/system/model/errors";
import EmptyObject from "ember-data/system/empty-object";

/**
@module ember-data
Expand Down Expand Up @@ -616,7 +617,7 @@ var Model = Ember.Object.extend(Ember.Evented, {
var currentData = get(this._internalModel, '_attributes');
var inFlightData = get(this._internalModel, '_inFlightAttributes');
var newData = merge(copy(inFlightData), currentData);
var diffData = Object.create(null);
var diffData = new EmptyObject();

var newDataKeys = Object.keys(newData);

Expand Down
4 changes: 2 additions & 2 deletions packages/ember-data/lib/system/model/states.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
@module ember-data
*/

import EmptyObject from "ember-data/system/empty-object";
var get = Ember.get;
/*
This file encapsulates the various states that a record can transition
Expand Down Expand Up @@ -354,7 +354,7 @@ var DirtyState = {
},

exit: function(internalModel) {
internalModel._inFlightAttributes = Object.create(null);
internalModel._inFlightAttributes = new EmptyObject();
}
}
};
Expand Down
3 changes: 2 additions & 1 deletion packages/ember-data/lib/system/relationships/ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Map,
MapWithDefault
} from "ember-data/system/map";
import EmptyObject from "ember-data/system/empty-object";

var get = Ember.get;

Expand Down Expand Up @@ -185,7 +186,7 @@ Model.reopenClass({
},

inverseMap: Ember.computed(function() {
return Object.create(null);
return new EmptyObject();
}),

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/ember-data/lib/system/relationships/state/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ManyRelationship from "ember-data/system/relationships/state/has-many";
import BelongsToRelationship from "ember-data/system/relationships/state/belongs-to";
import EmptyObject from "ember-data/system/empty-object";

var get = Ember.get;

Expand All @@ -20,7 +21,7 @@ function createRelationshipFor(record, relationshipMeta, store) {

export default function Relationships(record) {
this.record = record;
this.initializedRelationships = Object.create(null);
this.initializedRelationships = new EmptyObject();
}

Relationships.prototype.has = function(key) {
Expand Down
13 changes: 7 additions & 6 deletions packages/ember-data/lib/system/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@module ember-data
*/

import EmptyObject from "ember-data/system/empty-object";
var get = Ember.get;

/**
Expand All @@ -12,11 +13,11 @@ var get = Ember.get;
@param {DS.Model} internalModel The model to create a snapshot from
*/
export default function Snapshot(internalModel) {
this._attributes = Object.create(null);
this._belongsToRelationships = Object.create(null);
this._belongsToIds = Object.create(null);
this._hasManyRelationships = Object.create(null);
this._hasManyIds = Object.create(null);
this._attributes = new EmptyObject();
this._belongsToRelationships = new EmptyObject();
this._belongsToIds = new EmptyObject();
this._hasManyRelationships = new EmptyObject();
this._hasManyIds = new EmptyObject();

var record = internalModel.getRecord();
this.record = record;
Expand Down Expand Up @@ -135,7 +136,7 @@ Snapshot.prototype = {
@return {Object} All changed attributes of the current snapshot
*/
changedAttributes: function() {
let changedAttributes = Object.create(null);
let changedAttributes = new EmptyObject();
let changedAttributeKeys = Object.keys(this._changedAttributes);

for (let i=0, length = changedAttributeKeys.length; i < length; i++) {
Expand Down
10 changes: 6 additions & 4 deletions packages/ember-data/lib/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import ContainerInstanceCache from 'ember-data/system/store/container-instance-c

import InternalModel from "ember-data/system/model/internal-model";

import EmptyObject from "ember-data/system/empty-object";

var Backburner = Ember._Backburner || Ember.Backburner || Ember.__loader.require('backburner')['default'] || Ember.__loader.require('backburner')['Backburner'];

//Shim Backburner.join
Expand Down Expand Up @@ -319,7 +321,7 @@ Store = Service.extend({
createRecord: function(modelName, inputProperties) {
Ember.assert(`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${Ember.inspect(modelName)}`, typeof modelName === 'string');
var typeClass = this.modelFor(modelName);
var properties = copy(inputProperties) || Object.create(null);
var properties = copy(inputProperties) || new EmptyObject();

// If the passed properties do not include a primary key,
// give the adapter an opportunity to generate one. Typically,
Expand Down Expand Up @@ -1105,7 +1107,7 @@ Store = Service.extend({
record.destroy(); // maybe within unloadRecord
}

typeMap.metadata = Object.create(null);
typeMap.metadata = new EmptyObject();
}

function byType(entry) {
Expand Down Expand Up @@ -1421,9 +1423,9 @@ Store = Service.extend({
if (typeMap) { return typeMap; }

typeMap = {
idToRecord: Object.create(null),
idToRecord: new EmptyObject(),
records: [],
metadata: Object.create(null),
metadata: new EmptyObject(),
type: typeClass
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Ember from 'ember';
import EmptyObject from "ember-data/system/empty-object";

/**
* The `ContainerInstanceCache` serves as a lazy cache for looking up
Expand All @@ -20,10 +21,10 @@ import Ember from 'ember';
*/
export default function ContainerInstanceCache(container) {
this._container = container;
this._cache = Object.create(null);
this._cache = new EmptyObject();
}

ContainerInstanceCache.prototype = Object.create(null);
ContainerInstanceCache.prototype = new EmptyObject();

Ember.merge(ContainerInstanceCache.prototype, {
get: function(type, preferredKey, fallbacks) {
Expand Down