BSON vs JSON performance

3,947 views
Skip to first unread message

Marvin K

unread,
Feb 11, 2012, 2:06:59 AM2/11/12
to mongodb-user
I read that MongoDB stores data in BSON instead of JSON for
performance reasons. It's clear that binary data would take up less
disk space.

However, this guy did some tests and found BSON to be slower, in fact:
https://gist.github.com/263161

Any idea if BSON is actually slower than JSON?

Andreas Jung

unread,
Feb 11, 2012, 2:57:40 AM2/11/12
to mongod...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Marvin K wrote:
> I read that MongoDB stores data in BSON instead of JSON for
> performance reasons. It's clear that binary data would take up less
> disk space.
>
> However, this guy did some tests and found BSON to be slower, in
> fact: https://gist.github.com/263161
>

Does it matter in a real way for the real world?

BSON in the the internal database format and data must be serialized
or deserialized in every case.

- -aj
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQGUBAEBAgAGBQJPNh90AAoJEADcfz7u4AZjNeYLv2TirZmDCtl2N6cVyqGaZO7N
cSRcrAp89xAZfK5ihxtIcwCAqbY4sBVAYx5k6xvMo1AM6YSDUawNkqhVoSd2Mxtu
qv0VR5Wm9QocP+x19aAO3rmlOcTVWiupNpGHXOJavXwHjZyKx1Uh4lFlRNjY3kDR
fGcyjYtdYpQvfmWV4ehqpU7/sFdJM5t2RdiCyE3eciqpLqQZaX24b2TI6QL8jmZm
6FODlA+zbWojczHoBA4r0pP7U9RdtwA1NUDSRKHtBDWtWciirXFDq/1U6UdUI5ky
T3wpaKtMXLF5TAafMO5yKXEH6KWBJTY7aqBtL04Q19Qc15V0XdaqSFUuhJgLL9Mn
FTsYcA29WTFMLEUT9vlF0PYiCjQPBScuWa2v8SEfCkCy5pMDIOfy1KhNcQBkOqi5
nw7S46HWeAegOnSO1mioEFwG7EZhL7NkIao5WdwVgX75xP8/O1oyvMyvJFuYXrfL
qhqLRmOPgdD4Mqh97yQhioc8LdAEsJI=
=/Iwi
-----END PGP SIGNATURE-----

lists.vcf

Sam Millman

unread,
Feb 11, 2012, 6:27:09 AM2/11/12
to mongod...@googlegroups.com
I don't know Ruby but:

"I've created four tests, each to time how long JSON and BSON each take to generate their format from a Ruby Hash, and how long time they take to parse their data back into a Ruby Hash"

Means that the test is immediately void since he is not testing the storage of BSON but instead of serialization and storage.

"With Ruby 1.9.1 however, the story looks different. BSON is about five times faster than JSON at generating their respective formats with large (10,000 rows in 0.3 secongs) and small datasets. Parsing back to a Ruby Hash though, BSON is about twice as slow as JSON with large datasets."

There are more than one problem with this test:

- It is only on one language
- There is such huge variances between different versions of the language I am not actually sure this test is even valid.
- He is using the Mongodb implementation of BSON serialize which in Most drivers is still extremely beta and slow.
- Also he is not using the module alone but instead using it with the rest of Mongo module

Also you gotta remember that JSON and BSON are not the same, they do not store the same and they do not completely act the same: http://bsonspec.org/

Most namely BSON allows inbuilt data types (which yes does make it heavier) that are not supported in JSON along with other things.


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Scott Hernandez

unread,
Feb 11, 2012, 9:32:05 AM2/11/12
to mongod...@googlegroups.com
As others have said it depends on the language and parser/decoder
implementations used. It also depends what you are encoding/decoding
to and from -- some languages are much heavier than others when it
comes to object creation and such.

In general BSON was not developed explicitly to be smaller or faster
but more expressive and easier to encode/decode. The data types
available in BSON are a superset of JSON as seen here:
http://bsonspec.org/#/specification

Are you interested in using BSON outside of the database? If so there
are lots of people doing this in places where MongoDB is not involved
at all.

Marvin K

unread,
Feb 11, 2012, 4:00:52 PM2/11/12
to mongodb-user
Scott, yes, I was considering using BSON independent of the database.

I am going to be logging some stuff and thought about logging them as:

1) JSON objects appended to a text file, one per line
2) BSON objects appended to a binary file (how would you separate
them? I'm not sure...)
3) Use MongoDB

It's pretty clear that for my use (3) MongoDB is overbuilding the
problem. Plus there's some things I want to do that would be simpler
with the data in single files instead of in a DB.

I'd prefer BSON over JSON for space reasons, but don't want to forfeit
speed. It's OK if I have to do a little bit more coding to work with
binary.

How do you store a bunch of BSON objects in a file? A link to a
sample project in Python would be great, it'd give me something to
experiment with.

Eliot Horowitz

unread,
Feb 11, 2012, 10:17:30 PM2/11/12
to mongod...@googlegroups.com
mongodump/restore just store raw bson in files.
one document after another.
since a document tells you how large it is, easy to read a file iteratively.
Reply all
Reply to author
Forward
0 new messages