Skip to content

Commit

Permalink
Few forEach to for of loop
Browse files Browse the repository at this point in the history
Tiny speed improvement with loops.
  • Loading branch information
StorytellerCZ committed Aug 15, 2024
1 parent 230a115 commit e714afd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
4 changes: 2 additions & 2 deletions package/collection2/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function flattenSelector(selector) {

const obj = {};

Object.entries(selector).forEach(([key, value]) => {
for (const [key, value] of Object.entries(selector) || []) {
// Ignoring logical selectors (https://docs.mongodb.com/manual/reference/operator/query/#logical)
if (!key.startsWith('$')) {
if (typeof value === 'object' && value !== null) {
Expand All @@ -25,7 +25,7 @@ export function flattenSelector(selector) {
obj[key] = value;
}
}
});
}

return obj;
}
Expand Down
26 changes: 12 additions & 14 deletions package/collection2/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Mongo.Collection.prototype.attachSchema = function c2AttachSchema(ss, options) {
Collection2.emit('schema.attached', this, ss, options);
};

[Mongo.Collection, LocalCollection].forEach((obj) => {
for (const obj of [Mongo.Collection, LocalCollection]) {
/**
* simpleSchema
* @description function detect the correct schema by given params. If it
Expand All @@ -132,15 +132,15 @@ Mongo.Collection.prototype.attachSchema = function c2AttachSchema(ss, options) {
obj.prototype.simpleSchema = function (doc, options, query) {
if (!this._c2) return null;
if (this._c2._simpleSchema) return this._c2._simpleSchema;

const schemas = this._c2._simpleSchemas;
if (schemas && schemas.length > 0) {
let schema, selector, target;
// Position 0 reserved for base schema
for (let i = 1; i < schemas.length; i++) {
schema = schemas[i];
selector = Object.keys(schema.selector)[0];

// We will set this to undefined because in theory, you might want to select
// on a null value.
target = undefined;
Expand All @@ -156,7 +156,7 @@ Mongo.Collection.prototype.attachSchema = function c2AttachSchema(ss, options) {
// on upsert/update operations
target = query[selector];
}

// we need to compare given selector with doc property or option to
// find the right schema
if (target !== undefined && target === schema.selector[selector]) {
Expand All @@ -169,12 +169,12 @@ Mongo.Collection.prototype.attachSchema = function c2AttachSchema(ss, options) {
throw new Error('No default schema');
}
}

return null;
};
});
}

function getArgumentsAndValidationContext(methodName, args, async) {
function getArgumentsAndValidationContext(methodName, args, async) {
let options = isInsertType(methodName) ? args[1] : args[2];

// Support missing options arg
Expand Down Expand Up @@ -414,14 +414,12 @@ Mongo.Collection.prototype.attachSchema = function c2AttachSchema(ss, options) {
};

const cleanOptionsForThisOperation = {};
['autoConvert', 'filter', 'removeEmptyStrings', 'removeNullsFromArrays', 'trimStrings'].forEach(
(prop) => {
if (typeof options[prop] === 'boolean') {
cleanOptionsForThisOperation[prop] = options[prop];
}
for (const prop of ['autoConvert', 'filter', 'removeEmptyStrings', 'removeNullsFromArrays', 'trimStrings']) {
if (typeof options[prop] === 'boolean') {
cleanOptionsForThisOperation[prop] = options[prop];
}
);
}

// Preliminary cleaning on both client and server. On the server and for local
// collections, automatic values will also be set at this point.
schema.clean(doc, {
Expand Down
4 changes: 2 additions & 2 deletions package/collection2/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Package.describe({
name: 'aldeed:collection2',
summary:
'Automatic validation of Meteor Mongo insert and update operations on the client and server',
version: '4.0.3',
version: '4.0.4',
documentation: '../../README.md',
git: 'https://github.com/aldeed/meteor-collection2.git'
});
Expand All @@ -23,7 +23,7 @@ Package.onUse(function (api) {
api.use('ejson');
api.use('ecmascript');
api.use('raix:eventemitter@1.0.0');
api.use('aldeed:simple-schema@1.13.1 || 2.0.0-rc.300.10');
api.use('aldeed:simple-schema@1.13.1 || 2.0.0-rc300.1');

api.addFiles(['./collection2.js']);

Expand Down

0 comments on commit e714afd

Please sign in to comment.