Skip to content

Instantly share code, notes, and snippets.

@nataliemarleny
Created April 7, 2019 11:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nataliemarleny/d3399584827b5ce7d69735d77bdb5fe1 to your computer and use it in GitHub Desktop.
Save nataliemarleny/d3399584827b5ce7d69735d77bdb5fe1 to your computer and use it in GitHub Desktop.
Script to evaluate the percentage of the size of a pattern in the JavaScript files of a given url
#!/usr/bin/env bash
set -eu -o pipefail
# Script expects these binaries to be present:
# - wget
# - node
# Tested on Mac.
# Example:
#
# bash sizeOfStringInUrl.sh nextjs.org "\bcreateElement\b"
main() {
url="$1"
pattern="${2:-\bcreateElement\b}"
if [ -e "$url" ]; then
echo ">> Directory $url already exists... EXITING"
exit 1
fi
echo ">> Downloading $url"
mkdir "$url"
cd "$url"
wget -pk "$url"
count_occurrences=$(find . -name "*.js" -exec grep -o createElement {} \; | wc -l)
echo ">> Number of occurrences of \"$pattern\": $count_occurrences"
total_size_occurrences_with_newlines=$(find . -name "*.js" -exec grep -o createElement {} \; | wc -c)
total_size_occurrences=$(node -pe "$total_size_occurrences_with_newlines - $count_occurrences")
echo ">> Total size of occurrences in characters \"$pattern\": $total_size_occurrences"
echo ">> Assuming 1 character == 1 byte"
total_size_of_js_files_in_bytes=$(\
find . -iname "*.js" | \
xargs stat -f%z | \
awk '{ s+=$1 } END { print s }'\
)
echo ">> Total size of the JavaScript files in bytes: $total_size_of_js_files_in_bytes"
percentage=$(node -pe "100.0 * $total_size_occurrences / $total_size_of_js_files_in_bytes")
echo ">> Percentage size of \"$pattern\" in \"$url\": ${percentage}%"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment