Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Bitflags vs plain object <-> JSON
(version: 0)
Parsing performance of converting to/from serialized/developer-friendly representations of data
Comparing performance of:
BitFlags vs Plain object
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var BitFlags = { None: 0, One: 1 << 0, Two: 1 << 1, Three: 1 << 2, Four: 1 << 3, Five: 1 << 4, Six: 1 << 5, Seven: 1 << 6, Eight: 1 << 7, }; function parseFlags(f) { return { one: !!(f & BitFlags.One), two: !!(f & BitFlags.Two), three: !!(f & BitFlags.Three), four: !!(f & BitFlags.Four), five: !!(f & BitFlags.Five), six: !!(f & BitFlags.Six), seven: !!(f & BitFlags.Seven), eight: !!(f & BitFlags.Eight), }; } function toFlags(o) { var result = 0; if (o.one) result |= BitFlags.One; if (o.two) result |= BitFlags.Two; if (o.three) result |= BitFlags.Three; if (o.four) result |= BitFlags.Four; if (o.five) result |= BitFlags.Five; if (o.six) result |= BitFlags.Six; if (o.seven) result |= BitFlags.Seven; if (o.eight) result |= BitFlags.Eight; return result; }
Tests:
BitFlags
// Create var flags = BitFlags.One | BitFlags.Four | BitFlags.Six | BitFlags.Seven | BitFlags.Eight; // Read/Update if (flags & BitFlags.One) { flags |= BitFlags.Two; } // Deserialize var o = parseFlags(flags); // Serialize var f = toFlags(o);
Plain object
// Create var obj = { one: true, two: false, three: false, four: true, five: false, six: true, seven: true, eight: true, }; // Read/Update if (obj.one) { obj.two = true; } // Serialize var s = JSON.stringify(obj); // Deserialize var o = JSON.parse(s);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
BitFlags
Plain object
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Definition** The benchmark is testing the performance of converting between two data representations: BitFlags (a custom, bitwise enum-like representation) and plain objects (JavaScript objects). The goal is to measure which approach is faster when converting from one format to another. **Options Compared** Two approaches are being compared: 1. **BitFlags**: A custom representation using bitwise operations to store a set of flags. 2. **Plain object**: A JavaScript object with properties representing the same flags, where each property's value indicates whether the corresponding flag is set (true) or not (false). **Pros and Cons** **BitFlags:** Pros: * Can be more compact than plain objects when dealing with sparse sets of flags. * Can be useful in scenarios where memory usage is critical. Cons: * Requires manual management of bitwise operations, which can add complexity. * May require additional overhead for parsing and serialization. **Plain object:** Pros: * More human-readable and maintainable. * Easier to work with and understand, especially for developers without expertise in bitwise operations. Cons: * May result in larger memory usage compared to BitFlags when dealing with sparse sets of flags. * Can be slower due to the overhead of JavaScript object creation and property access. **Library** The `parseFlags` function is using a custom library (not part of the standard JavaScript) to parse and serialize the BitFlags representation. This library provides a way to convert between the bitwise enum-like format and a more human-readable representation, making it easier to work with BitFlags. **Special JS Feature/Syntax** This benchmark uses no special JavaScript features or syntax beyond what's considered standard (ES5/6). The focus is on comparing two data representations rather than exploiting specific language features. **Other Alternatives** Other alternatives could be: * Using a more compact representation, such as a single byte or bit in a specific format. * Implementing a binary serialization library like `JSON.stringify` and `JSON.parse`, which can also serialize bitwise enums. * Comparing the performance of different serialization libraries or formats (e.g., Protocol Buffers, MessagePack). Keep in mind that these alternatives might not directly address the core question being asked by this benchmark, but they could provide additional insights into data representation efficiency.
Related benchmarks:
floor() vs trunc() vs bitwise hacks (~~, >> 0, etc)
Ramda vs. Lodash 2
Ramda vs. Lodash 3
Ramda vs. Lodash 6
floor() vs trunc() vs bitwise hacks (~~, >> 0, etc) 2
Comments
Confirm delete:
Do you really want to delete benchmark?