Web Excursions 2021-11-26
How Can You Compare the Performance of M1 Chips? 2 Core Allocation
In the original M1, macOS recruits P and E cores individually per process.
However, in the M1 Pro it recruits cores by cluster,
first loading cores 3-6 in the first P cluster,
then 7-10 in the second P cluster, and finally 1 and 2 in the E cluster.
At minimum QoS, for background services,
no P cores are recruited on either M1 chip, and
the load is confined to the E cores,
where it’s shared fairly equally.
In some respects (at least), performance of an E core in the M1 Pro may attain 70% that of a P core.
How to Work With JSON API Data in macOS Shell Scripts Without External Dependencies | Paul Galow
Since a shell script is our main execution context, we are handing off as much control as possible to our shell environment
by passing and expanding shell variables into a JavaScript expression.
We can then evaluate this expression using
osascript -l 'JavaScript'
.
So a minimal implementation would look something like this:
osascript -l 'JavaScript' -e "($1.$2);"
Here, all we do is implicitly return a key’s value (defined in
$2
)of whatever variable has been passed into the JavaScript execution context by our shell runtime (
$1
).We don’t even need to use the
return
keyword or JXA’s specialrun()
function.
[Potential security ramifications and the solution]
by not using
JSON.parse()
we are enabling a potential backdoor for a remote code execution attack,because without it, any JavaScript code injected will be executed, not just JSON
Using [
JSON.parse()
], we make sure to parse valid JSON only.If the incoming string is not JSON, this method call will throw an error.
But backticks are special characters in shell scripts
So we need to escape them (using backslashes) to ensure our shell runtime does not evaluate them as shell expressions.
E.g., retrive album release date from the iTunes API:
getJsonValue() {
# $1: JSON string to process, $2: Desired JSON key
osascript -l 'JavaScript' -e "JSON.parse(\`$1\`).$2;"
}
data=$(curl -sS 'https://itunes.apple.com/search?term=steely+dan+pretzel+logic&entity=album')
releaseDate=$(getJsonValue "$data" "results[0].releaseDate")
echo "$releaseDate" # Returns: '1974-01-01T08:00:00Z'