From d88406a31bcd8fdf0b57b078f322d3cf6c62333d Mon Sep 17 00:00:00 2001 From: Yulong Wang <7679871+fs-eire@users.noreply.github.com> Date: Tue, 5 Sep 2023 23:14:46 -0700 Subject: [PATCH] [js/common] use Map instead of object for backends (#17352) ### Description resolved https://github.com/microsoft/onnxruntime/security/code-scanning/1140 --- js/common/lib/backend-impl.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/js/common/lib/backend-impl.ts b/js/common/lib/backend-impl.ts index 57488e1642..75feba1d0a 100644 --- a/js/common/lib/backend-impl.ts +++ b/js/common/lib/backend-impl.ts @@ -12,7 +12,7 @@ interface BackendInfo { aborted?: boolean; } -const backends: {[name: string]: BackendInfo} = {}; +const backends: Map = new Map(); const backendsSortedByPriority: string[] = []; /** @@ -27,9 +27,9 @@ const backendsSortedByPriority: string[] = []; */ export const registerBackend = (name: string, backend: Backend, priority: number): void => { if (backend && typeof backend.init === 'function' && typeof backend.createSessionHandler === 'function') { - const currentBackend = backends[name]; + const currentBackend = backends.get(name); if (currentBackend === undefined) { - backends[name] = {backend, priority}; + backends.set(name, {backend, priority}); } else if (currentBackend.priority > priority) { // same name is already registered with a higher priority. skip registeration. return; @@ -46,7 +46,7 @@ export const registerBackend = (name: string, backend: Backend, priority: number } for (let i = 0; i < backendsSortedByPriority.length; i++) { - if (backends[backendsSortedByPriority[i]].priority <= priority) { + if (backends.get(backendsSortedByPriority[i])!.priority <= priority) { backendsSortedByPriority.splice(i, 0, name); return; } @@ -71,7 +71,7 @@ export const resolveBackend = async(backendHints: readonly string[]): Promise