For a rough estimate, you can calculate that the file size is typically around 75% of the base64 string's size. The actual size would not exceed this estimate and could be up to two bytes smaller.
If you prefer a quick solution, you can simply use atob()
and check the length as suggested in other responses.
However, for precise measurements with optimal performance (especially important for handling massive files or multiple files), consider factoring in padding when calculating the exact size:
let base64Length = src.length - (src.indexOf(',') + 1);
let padding = (src.charAt(src.length - 2) === '=') ? 2 : ((src.charAt(src.length - 1) === '=') ? 1 : 0);
let fileSize = base64Length * 0.75 - padding;
This method avoids parsing the entire string and should only be used if seeking extreme optimization or facing memory constraints.