Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Bitwise vs Boolean data handling
(version: 0)
Comparing performance of:
Bitwise vs Boolean
Created:
7 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function CompletedCourses() { this.init = function() { this._state = new Array(COURSES.length).fill(false); } this.completeCourse = function(index) { this._state[index] = true; } this.getCompletedCourses = function() { const completedCourses = COURSES.filter((course, index) => this._state[index]); return completedCourses; } this.init(); return this; } function CompletedCoursesBits() { this.initBits = function() { // using bits this._status = 0; } this.completeCourseBit = function(index) { this._status = this._status | 1 << index; } this.getCompletedCoursesByBits = function() { const completedCourseWithBits = COURSES.filter((course, index) => (this._status & 1 << index)); return completedCourseWithBits; } this.initBits(); return this; } function Student(name, completedCourses) { this.name = name; this.courses = completedCourses; } var COURSES = [ 'English', 'Chemistry', 'Physics', 'Literature', 'Arts', 'Algabra', 'Infi', 'Algorithms', 'DataStructures', 'Complexity' ]; var n = 10000;
Tests:
Bitwise
function startBits() { // create const students = []; for (let i = 0; i < n; i++) { students.push(new Student('Mike', new CompletedCoursesBits())); } for (let i = 0; i < n; i++) { const student = students[i]; COURSES.forEach((course, index) => index & 1 ? student.courses.completeCourseBit(index) : '') } // log const list = []; for (let i = 0; i < n; i++) { const student = students[i]; list.push(student.courses.getCompletedCoursesByBits()); } } startBits();
Boolean
function start(){ // create const students = []; for (let i = 0; i < n; i++) { students.push(new Student('Mike', new CompletedCourses())); } // populate for (let i = 0; i < n; i++) { const student = students[i]; COURSES.forEach((course, index) => index & 1 ? student.courses.completeCourse(index) : '') } // log const list = []; for (let i = 0; i < n; i++) { const student = students[i]; list.push(student.courses.getCompletedCourses()); } } start();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Bitwise
Boolean
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):
I'll break down the provided JSON benchmark definition, explain what's being tested, and discuss the pros and cons of each approach. **Benchmark Definition** The benchmark tests two different approaches to handle completion data for courses: using bitwise operations (Bitwise) versus using boolean values (Boolean). **CompletedCourses Class** This class is used in both test cases. It has three methods: 1. `init`: Initializes the `_state` array, which stores the completion status of each course. 2. `completeCourse(index)`: Sets the completion status of a specific course to true at the specified index in the `_state` array. 3. `getCompletedCourses(index)`: Returns an array of courses completed by the student at the specified index. **CompletedCoursesBits Class** This class is used only in the Bitwise test case. It has three methods: 1. `initBits()`: Initializes the `_status` variable, which stores the completion status using bitwise operations. 2. `completeCourseBit(index)`: Sets the completion status of a specific course to true by performing a bitwise OR operation on the `_status` variable. 3. `getCompletedCoursesByBits()`: Returns an array of courses completed by the student at each index by performing a bitwise AND operation between the `_status` variable and a mask. **Test Cases** There are two test cases: 1. **Bitwise**: This test case uses the CompletedCoursesBits class to complete courses using bitwise operations. It creates `n` students, each with an instance of CompletedCoursesBits, and then completes courses for each student using the `completeCourseBit()` method. Finally, it retrieves the completed courses by performing a bitwise AND operation on the `_status` variable. 2. **Boolean**: This test case uses the CompletedCourses class to complete courses using boolean values. It creates `n` students, each with an instance of CompletedCourses, and then completes courses for each student using the `completeCourse()` method. Finally, it retrieves the completed courses by calling the `getCompletedCourses()` method. **Pros and Cons** **Bitwise Approach:** Pros: * Can potentially be faster due to bitwise operations being more efficient than boolean comparisons. * May use less memory since the `_status` variable can store multiple completion statuses in a single byte. Cons: * Requires additional effort to manage the bit mask, which can increase complexity. * May not be as readable or maintainable for those unfamiliar with bitwise operations. **Boolean Approach:** Pros: * More intuitive and easier to understand for developers without experience with bitwise operations. * Requires less additional effort to implement and maintain. Cons: * May be slower due to the overhead of boolean comparisons. **Other Considerations** Both approaches have their trade-offs. The Bitwise approach may offer a performance advantage, but requires more expertise in bitwise operations. The Boolean approach is more readable and maintainable, but may come at the cost of slower execution. **Library Used** The `filter()` method is used in both test cases to retrieve completed courses. This is a built-in JavaScript array method that returns a new array with all elements that pass the test implemented by the provided function. There are no special JS features or syntax explicitly mentioned in this benchmark definition.
Related benchmarks:
Array filter falsy
ES6 spread vs. lodash assign
filter with Boolean
test array filter vs object destructure deux
Lodash.filter vs Native.filter
Comments
Confirm delete:
Do you really want to delete benchmark?