79423599

Date: 2025-02-08 17:16:57
Score: 0.5
Natty:
Report link

Did what @Socowi suggested to test it out using zsh on an M1 Mac. Turned out that using an associative array should be the fastest way.

Here's what I did:

# Prepare test data.
size=$((10 ** 6))
normal_array=($(seq -f 'text%.0f' 1 "$size"))
plaintext="$(printf "%s\n" "${normal_array[@]}")"
typeset -A associative_array
for elem in "${normal_array[@]}"; do associative_array[$elem]=1; done

# Test Suite #1
printf %s$'\n' "${normal_array[@]}" | grep -Fx "gibberish"
( IFS=$'\n'; echo "${normal_array[*]}" ) | grep -Fx "gibberish"
[[ -v associative_array[gibberish] ]] && echo $associative_array[gibberish]

# Test Suite #2
printf %s$'\n' "${normal_array[@]}" | grep -Fx "text900000"
( IFS=$'\n'; echo "${normal_array[*]}" ) | grep -Fx "text900000"
[[ -v associative_array[text900000] ]] && echo $associative_array[text900000]

The results were self-explanatory: The associative array consistently outperformed the rest, i.e. 5ms 🆚 300ms+

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Starts with a question (0.5): Did
  • Low reputation (1):
Posted by: Paid in Cache