From 4ef6f1ab2eb43fc69ed1acb8c380f4877afa1d4b Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Fri, 6 Dec 2019 20:19:02 -0800 Subject: [PATCH] refactor(ivy): prevent `-0` from being generated (#34290) PR Close #34290 --- packages/core/src/render3/instructions/shared.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/core/src/render3/instructions/shared.ts b/packages/core/src/render3/instructions/shared.ts index c61daf9d48..d0bdbac933 100644 --- a/packages/core/src/render3/instructions/shared.ts +++ b/packages/core/src/render3/instructions/shared.ts @@ -1216,7 +1216,10 @@ export function generateExpandoInstructionBlock( tView.firstCreatePass, true, 'Expando block should only be generated on first create pass.'); - const elementIndex = -(tNode.index - HEADER_OFFSET); + // Important: In JS `-x` and `0-x` is not the same! If `x===0` then `-x` will produce `-0` which + // requires non standard math arithmetic and it can prevent VM optimizations. + // `0-0` will always produce `0` and will not cause a potential deoptimization in VM. + const elementIndex = HEADER_OFFSET - tNode.index; const providerStartIndex = tNode.providerIndexes & TNodeProviderIndexes.ProvidersStartIndexMask; const providerCount = tView.data.length - providerStartIndex; (tView.expandoInstructions || (tView.expandoInstructions = [