Optimization for empty arrays.

This commit is contained in:
Julien Fontanet 2015-06-23 09:13:43 +02:00
parent 9cb4de2ea8
commit bb1ea4e4d0

View File

@ -125,6 +125,10 @@ let getNotConnectedPromise = function () {
const OPAQUE_REF_RE = /^OpaqueRef:/ const OPAQUE_REF_RE = /^OpaqueRef:/
// -------------------------------------------------------------------
const EMPTY_ARRAY = []
// =================================================================== // ===================================================================
const MAX_TRIES = 5 const MAX_TRIES = 5
@ -381,24 +385,28 @@ export class Xapi extends EventEmitter {
// Creates resolved properties. // Creates resolved properties.
forEach(object, function resolveObject (value, key, object) { forEach(object, function resolveObject (value, key, object) {
if (isArray(value)) { if (isArray(value)) {
// Do not create an array of links unless it is known this is if (!value.length) {
// an array of refs. // If the array is empty, it isn't possible to be sure that
if (value.length && !OPAQUE_REF_RE.test(value)) { // it is not supposed to contain links, therefore, in
return // benefice of the doubt, a resolved property is defined.
} defineProperty(object, '$' + key, {
value: EMPTY_ARRAY
})
defineProperty(object, '$' + key, { // Minor memory optimization, use the same empty array for
get () { // everyone.
return map(value, (ref) => objectsByRefs[ref]) object[key] = EMPTY_ARRAY
} } else if (OPAQUE_REF_RE.test(value)) {
}) // This is an array of refs.
defineProperty(object, '$' + key, {
get: () => map(value, (ref) => objectsByRefs[ref])
})
}
} else if (isObject(value)) { } else if (isObject(value)) {
forEach(value, resolveObject) forEach(value, resolveObject)
} else if (OPAQUE_REF_RE.test(value)) { } else if (OPAQUE_REF_RE.test(value)) {
defineProperty(object, '$' + key, { defineProperty(object, '$' + key, {
get () { get: () => objectsByRefs[value]
return objectsByRefs[value]
}
}) })
} }
}) })