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

[ML] Improving empty object creation in ML packages #191901

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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function getFieldValuePairCounts(cpgs: SignificantItemGroup[]): FieldValu
return cpgs.reduce<FieldValuePairCounts>((p, { group }) => {
group.forEach(({ fieldName, fieldValue }) => {
if (p[fieldName] === undefined) {
p[fieldName] = {};
p[fieldName] = Object.create(null);
}
p[fieldName][fieldValue] = p[fieldName][fieldValue] ? p[fieldName][fieldValue] + 1 : 1;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ describe('isPopulatedObject', () => {
])
).toBe(false);
});
it('does not allow an object with a required attribute in the prototype ', () => {
const testObject = { attribute: 'value', __proto__: { otherAttribute: 'value' } };
expect(isPopulatedObject(testObject, ['otherAttribute'])).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const isPopulatedObject = <U extends string = string, T extends unknown =
typeof arg === 'object' &&
arg !== null &&
Object.keys(arg).length > 0 &&
(requiredAttributes.length === 0 ||
requiredAttributes.every((d) => ({}.hasOwnProperty.call(arg, d))))
(requiredAttributes.length === 0 || requiredAttributes.every((d) => Object.hasOwn(arg, d)))
);
};
2 changes: 1 addition & 1 deletion x-pack/packages/ml/json_schemas/src/json_schema_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class JsonSchemaService {
};
}

private allComponents: Record<string, object> = {};
private allComponents: Record<string, object> = Object.create(null);
private componentsDict = new Set<string>();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const setNestedProperty = (obj: Record<string, any>, accessor: string, va
for (let i = 0; i < len - 1; i++) {
const attribute = accessors[i];
if (typeof ref[attribute] !== 'object') {
ref[attribute] = {};
ref[attribute] = Object.create(null);
}

ref = ref[attribute];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export class RandomSampler {
const mode = this.getMode();
const probability = this.getProbability();

let prob = {};
let prob = Object.create(null);
if (mode === RANDOM_SAMPLER_OPTION.ON_MANUAL) {
prob = { probability };
} else if (mode === RANDOM_SAMPLER_OPTION.OFF) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function getCombinedRuntimeMappings(
dataView: DataView | undefined,
runtimeMappings?: RuntimeMappings
): RuntimeMappings | undefined {
let combinedRuntimeMappings = {};
let combinedRuntimeMappings = Object.create(null);

// Add runtime field mappings defined by index pattern
if (dataView) {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/packages/ml/url_state/src/url_state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function isRisonSerializationRequired(queryParam: string): boolean {
}

export function parseUrlState(search: string): Dictionary<any> {
const urlState: Dictionary<any> = {};
const urlState: Dictionary<any> = Object.create(null);
const parsedQueryString = parse(search, { sort: false });

try {
Expand Down Expand Up @@ -125,7 +125,7 @@ export const UrlStateProvider: FC<PropsWithChildren<unknown>> = ({ children }) =
const parsedQueryString = parse(prevSearchString, { sort: false });

if (!Object.hasOwn(urlState, accessor)) {
urlState[accessor] = {};
urlState[accessor] = Object.create(null);
}

if (typeof attribute === 'string') {
Expand Down