Skip to content

Misleading bsonjs.loads benchmark #67

@oleksii-symon-corva-ai

Description

@oleksii-symon-corva-ai

loads benchmark uses following logic:

    compare("bsonjs.loads(j)",
            "bson.BSON().encode(json_util.loads(j))",
            iterations,
            setup)
  1. Comparing bsonjs.loads(j) to bson.BSON().encode(json_util.loads(j)) is unfair. bson.BSON().encode is supposed to encode python dicts, so we should pass it a dict directly instead of loading from json before passing.
# Before
bson.BSON().encode(json_util.loads(j))
# After
bson.BSON().encode(doc)
  1. Using bson.BSON().encode is not recommeded, because of its additional performance cost, benchmark should use bson.encode method directly:
Image
# Before
bson.BSON().encode(doc)
# After
bson.encode(doc)

Final code:

# loads
compare('bsonjs.loads(j)', 'bson.encode(doc)', iterations, setup)

Test results

If we run benchmark with fixed code we get following results:

Mac M3

Timing: bsonjs.dumps(b)
10000 loops, best of 3: 0.03492570900000001
Timing: json_util.dumps(bson.decode(b))
10000 loops, best of 3: 0.2038257080000001
bsonjs is 5.84x faster than json_util

Timing: bsonjs.loads(j)
10000 loops, best of 3: 0.03936475000000006
Timing: bson.encode(doc)
10000 loops, best of 3: 0.014574333999999967
bsonjs is 0.37x faster than json_util

Mac Intel

Timing: bsonjs.dumps(b)
10000 loops, best of 3: 0.08610098099999997
Timing: json_util.dumps(bson.decode(b))
10000 loops, best of 3: 0.6389648219999999
bsonjs is 7.42x faster than json_util

Timing: bsonjs.loads(j)
10000 loops, best of 3: 0.08243793199999949
Timing: bson.encode(doc)
10000 loops, best of 3: 0.035725252999999846
bsonjs is 0.43x faster than json_util

Summary

Looks like in reality bsonjs.loads is more than 2 times slower than bson.encode.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions