Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Performance Concerns: Sqids significantly slower than HashIds in benchmarks #38

Pouria7 started this conversation in General
Discussion options

Performance Concerns: Sqids significantly slower than HashIds in benchmarks

Hello Sqids team,

I recently conducted a performance benchmark comparing Sqids with HashIds (ullmark/hashids.net), and I wanted to share my findings with you. While I appreciate the new features and improvements Sqids brings over HashIds, I've noticed a significant performance difference that I believe is worth discussing.

Benchmark Results

Here are the results of my benchmark test:

Method Mean Error StdDev Median Ratio RatioSD Allocated Alloc Ratio
HashId 4.109 μs 0.0715 μs 0.0634 μs 4.109 μs 1.00 0.00 7.2 KB 1.00
Sqids 54.574 μs 1.4042 μs 4.1182 μs 53.410 μs 12.20 0.50 74.22 KB 10.31

Observations

  1. Execution Time: Sqids is approximately 12 times slower than HashIds (mean execution time of 54.574 μs vs 4.109 μs).
  2. Memory Allocation: Sqids allocates about 10 times more memory than HashIds (74.22 KB vs 7.2 KB).

These benchmarks were conducted using both encode and decode operations together. However, I observed similar results when testing the encode operation alone.

Questions and Concerns

  1. Are you aware of this performance difference?
  2. Are there any known reasons for this significant gap in performance?
  3. Are there plans to optimize Sqids for better performance in future releases?
  4. Do you have any recommendations for users who require performance similar to HashIds while benefiting from Sqids' features?

I appreciate the work you've done on Sqids and I'm looking forward to hearing your thoughts on these performance issues. If you need any additional information or if you'd like me to run more specific benchmarks, please let me know.

You must be logged in to vote

Replies: 3 comments

Comment options

Hey @Pouria7, thanks for your efforts. Could you please share the actual benchmark code?

You must be logged in to vote
0 replies
Comment options


[MemoryDiagnoser(false)]
public class IdGeneratorBenchmark
{
 private int id = 5732;
 [Benchmark(Baseline = true)]
 public string HashId()
 {
 Hashids hashids = new Hashids();
 string encodedId = hashids.Encode(id);
 var decodedId = hashids.DecodeSingle(encodedId);
 //var decodedIds = hashids.Decode(result);
 return encodedId + decodedId.ToString();
 }
 [Benchmark]
 public string Sqids()
 {
 var sqd = new SqidsEncoder<int>();
 string encodedId = sqd.Encode(id);
 var decodedIds = sqd.Decode(encodedId);
 return encodedId + decodedIds[0].ToString();
 }
}
You must be logged in to vote
0 replies
Comment options

I realized that the initial benchmarks might have been affected by the time taken to create instances of the encoders. To address this, I modified the setup to create instances only once:

[GlobalSetup]
public void Setup()
{
 hashids = new Hashids();
 sqids = new SqidsEncoder<int>();
}

Updated Benchmark Results

After making these changes, here are the new benchmark results:

Method Mean Error StdDev Ratio RatioSD Allocated Alloc Ratio
HashId 928.2 ns 3.58 ns 2.99 ns 1.00 0.00 104 B 1.00
Sqids 2,558.4 ns 47.31 ns 44.25 ns 2.75 0.05 3040 B 29.23
  1. Execution Time: Sqids is now approximately 2.75 times slower than HashIds (mean execution time of 2,558.4 ns vs 928.2 ns). This is a significant improvement from the previous results but still shows a noticeable performance gap.

  2. Memory Allocation: Sqids still allocates significantly more memory than HashIds (3040 B vs 104 B), with an allocation ratio of about 29.23.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /