Skip to content

Commit 134000f

Browse files
authored
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
1 parent 489f5fd commit 134000f

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

crates/swc_ecma_minifier/examples/minify-all.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
extern crate swc_malloc;
44

5-
use std::{env, fs, path::PathBuf, time::Instant};
5+
use std::{env, path::PathBuf, time::Instant};
66

77
use anyhow::Result;
88
use rayon::prelude::*;
@@ -55,20 +55,26 @@ fn expand_dirs(dirs: Vec<String>) -> Vec<PathBuf> {
5555
.collect()
5656
}
5757

58-
struct Worker;
58+
struct Worker {
59+
total_size: usize,
60+
}
5961

6062
impl Parallel for Worker {
6163
fn create(&self) -> Self {
62-
Worker
64+
Worker { total_size: 0 }
6365
}
6466

65-
fn merge(&mut self, _: Self) {}
67+
fn merge(&mut self, other: Self) {
68+
self.total_size += other.total_size;
69+
}
6670
}
6771

6872
#[inline(never)] // For profiling
6973
fn minify_all(files: &[PathBuf]) {
7074
GLOBALS.set(&Default::default(), || {
71-
Worker.maybe_par(2, files, |_, path| {
75+
let mut worker = Worker { total_size: 0 };
76+
77+
worker.maybe_par(2, files, |worker, path| {
7278
testing::run_test(false, |cm, handler| {
7379
let fm = cm.load_file(path).expect("failed to load file");
7480

@@ -114,12 +120,14 @@ fn minify_all(files: &[PathBuf]) {
114120

115121
let code = print(cm.clone(), &[output], true);
116122

117-
fs::write("output.js", code.as_bytes()).expect("failed to write output");
123+
worker.total_size += code.len();
118124

119125
Ok(())
120126
})
121127
.unwrap()
122128
});
129+
130+
eprintln!("Total size: {}", worker.total_size);
123131
});
124132
}
125133

0 commit comments

Comments
 (0)