diff --git a/tfjs-core/src/backends/non_max_suppression_impl.ts b/tfjs-core/src/backends/non_max_suppression_impl.ts index e08108cc163..caa3c5bb27f 100644 --- a/tfjs-core/src/backends/non_max_suppression_impl.ts +++ b/tfjs-core/src/backends/non_max_suppression_impl.ts @@ -71,11 +71,15 @@ function nonMaxSuppressionImpl_( returnScoresTensor = false, padToMaxOutputSize = false): NamedTensorMap { // The list is sorted in ascending order, so that we can always pop the // candidate with the largest score in O(1) time. - const candidates = - Array.from(scores) - .map((score, boxIndex) => ({score, boxIndex, suppressBeginIndex: 0})) - .filter(c => c.score > scoreThreshold) - .sort(ascendingComparator); + const candidates = []; + + for (let i = 0; i < scores.length; i++) { + if (scores[i] > scoreThreshold) { + candidates.push({ score: scores[i], boxIndex: i, suppressBeginIndex: 0 }); + } + } + + candidates.sort(ascendingComparator); // If softNmsSigma is 0, the outcome of this algorithm is exactly same as // before.