From 2de466331a586746c8db9136a43ee6c1f4bc01f9 Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Fri, 16 Jun 2017 15:37:37 +0800 Subject: [PATCH] in-place `eliminate_spurious_blocks()` --- lib/compress.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 3231ffc2..944ad5db 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -668,7 +668,7 @@ merge(Compressor.prototype, { var CHANGED, max_iter = 10; do { CHANGED = false; - statements = eliminate_spurious_blocks(statements); + eliminate_spurious_blocks(statements); if (compressor.option("dead_code")) { eliminate_dead_code(statements, compressor); } @@ -890,25 +890,27 @@ merge(Compressor.prototype, { function eliminate_spurious_blocks(statements) { var seen_dirs = []; - return statements.reduce(function(a, stat){ + for (var i = 0; i < statements.length;) { + var stat = statements[i]; if (stat instanceof AST_BlockStatement) { CHANGED = true; - a.push.apply(a, eliminate_spurious_blocks(stat.body)); + eliminate_spurious_blocks(stat.body); + [].splice.apply(statements, [i, 1].concat(stat.body)); + i += stat.body.length; } else if (stat instanceof AST_EmptyStatement) { CHANGED = true; + statements.splice(i, 1); } else if (stat instanceof AST_Directive) { if (seen_dirs.indexOf(stat.value) < 0) { - a.push(stat); + i++; seen_dirs.push(stat.value); } else { CHANGED = true; + statements.splice(i, 1); } - } else { - a.push(stat); - } - return a; - }, []); - }; + } else i++; + } + } function handle_if_return(statements, compressor) { var self = compressor.self();