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;
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();
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();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Bitwise | |
Boolean |
Test name | Executions per second |
---|---|
Bitwise | 417.1 Ops/sec |
Boolean | 297.8 Ops/sec |
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:
init
: Initializes the _state
array, which stores the completion status of each course.completeCourse(index)
: Sets the completion status of a specific course to true at the specified index in the _state
array.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:
initBits()
: Initializes the _status
variable, which stores the completion status using bitwise operations.completeCourseBit(index)
: Sets the completion status of a specific course to true by performing a bitwise OR operation on the _status
variable.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:
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.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:
_status
variable can store multiple completion statuses in a single byte.Cons:
Boolean Approach:
Pros:
Cons:
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.