class Point {
constructor(x, y){
this.x = x;
this.y = y;
}
add(point){
return new Point(this.x + point.x, this.y + point.y);
}
sub(point){
return new Point(this.x - point.x, this.y - point.y);
}
toFloat32Array(){
return new Float32Array([this.x,this.y])
}
}
var p1 = new Point(10, 10);
var p2 = new Point(10, -10);
var sum = p1.add(p2);
var dif = p1.sub(p2);
var f32 = p1.toFloat32Array();
function create_point(x,y){
return new Float32Array([ x , y ]);
}
function add_points(a,b){
return new Float32Array( [ a[0] + b[0] , a[1] + b[1] ] );
}
function sub_points(a,b){
return new Float32Array( [ a[0] - b[0] , a[1] - b[1] ] );
}
var p1 = create_point(10,10);
var p2 = create_point(10,-10);
var sum = add_points(p1,p2);
var dif = sub_points(p1,p2);
var f32 = p1
function create_point(x,y){
return [ x , y ];
}
function add_points(a,b){
return [ a[0] + b[0] , a[1] + b[1] ];
}
function sub_points(a,b){
return [ a[0] - b[0] , a[1] - b[1] ];
}
function to_32(a){
return new Float32Array(a);
}
var p1 = create_point(10,10);
var p2 = create_point(10,-10);
var sum = add_points(p1,p2);
var dif = sub_points(p1,p2);
var f32 = to_32(p1);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
ES6 Class | |
Function float32array | |
Function array |
Test name | Executions per second |
---|---|
ES6 Class | 287064.7 Ops/sec |
Function float32array | 1069817.8 Ops/sec |
Function array | 4028283.5 Ops/sec |
The benchmark you provided evaluates three different approaches to managing and operating on 2D points in JavaScript: using ES6 classes, using functions with Float32Array
, and using functions with regular arrays.
ES6 Class Approach:
Point
is created. It has a constructor to initialize x and y coordinates, and methods for addition and subtraction of points, as well as a method to convert the points to a Float32Array
.add
, sub
, and toFloat32Array
methods are used to perform operations.Function with Float32Array:
Float32Array
to create points, add, and subtract them.Float32Array
instances which are efficient for mathematical computations.Float32Array
can be faster and more memory efficient, especially for large datasets.Function with Array:
Float32Array
.The benchmark results indicate the number of executions per second for each method:
Float32Array
or arrays with optimization may be preferred.Int32Array
or Uint8Array
, that could be used based on the data requirements.In summary, this benchmark compares three ways of managing 2D points using different data structures, focusing on performance, code clarity, and maintainability, allowing developers to make informed choices based on their specific use case.