From 134000fe93029c3af887726e5be6da1fd812e330 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sun, 19 Jan 2025 16:39:17 +0900 Subject: [PATCH] feat(es/minifier): Print total size from `minify-all` example (#9897) **Description:** I need this information to know if an optimization is worth the cost --- .../swc_ecma_minifier/examples/minify-all.rs | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/crates/swc_ecma_minifier/examples/minify-all.rs b/crates/swc_ecma_minifier/examples/minify-all.rs index 5c3b404bfb2d..b2a1f1b4ed27 100644 --- a/crates/swc_ecma_minifier/examples/minify-all.rs +++ b/crates/swc_ecma_minifier/examples/minify-all.rs @@ -2,7 +2,7 @@ extern crate swc_malloc; -use std::{env, fs, path::PathBuf, time::Instant}; +use std::{env, path::PathBuf, time::Instant}; use anyhow::Result; use rayon::prelude::*; @@ -55,20 +55,26 @@ fn expand_dirs(dirs: Vec) -> Vec { .collect() } -struct Worker; +struct Worker { + total_size: usize, +} impl Parallel for Worker { fn create(&self) -> Self { - Worker + Worker { total_size: 0 } } - fn merge(&mut self, _: Self) {} + fn merge(&mut self, other: Self) { + self.total_size += other.total_size; + } } #[inline(never)] // For profiling fn minify_all(files: &[PathBuf]) { GLOBALS.set(&Default::default(), || { - Worker.maybe_par(2, files, |_, path| { + let mut worker = Worker { total_size: 0 }; + + worker.maybe_par(2, files, |worker, path| { testing::run_test(false, |cm, handler| { let fm = cm.load_file(path).expect("failed to load file"); @@ -114,12 +120,14 @@ fn minify_all(files: &[PathBuf]) { let code = print(cm.clone(), &[output], true); - fs::write("output.js", code.as_bytes()).expect("failed to write output"); + worker.total_size += code.len(); Ok(()) }) .unwrap() }); + + eprintln!("Total size: {}", worker.total_size); }); }