Git Product home page Git Product logo

array-of-structs's Introduction

Array of Structs (AoS)

Typescript library for buffer backed array of structs.

-supports SharedArrayBuffers.

Installation

npm i array-of-structs

Usage

full example with iteration over elements:

const pos_meta = View.create_meta({
    x: Types.f32, 
    y: Types.f32
});

const [pos_view, pos_arr] = ArrayStruct.create(pos_meta);

//sets elements
for(let i=0; i < 100; i++){
    let index = pos_arr.add_one();
    pos_view.x[index] = 10;
    pos_view.y[index] = 20;
}

//iterating over elements. iterate attributes are important
for(let i=0; i < pos_arr.iterate_length; i += pos_arr.iterate_step){
    console.log(`x: ${pos_view.x[i]} y: ${pos_view.y[i]}`);
}


//important: the fith element gets replaces with the last element in array
pos_arr.delete(5);

create meta object descriptions (attributes need same byte sizes):

const player_meta = View.create_meta({
    x: Types.f32,
    y: Types.f32,
    hp: Types.i32
});

attach view to any Arraybuffer / Sharedarraybuffer:

const player_view = View.attach_meta(player_meta, new ArrayBuffer(1000));
//sets index 5
player_view.hp[5 * player_view[$size_of]] = 100;
player_view.x[5 * player_view[$size_of]] = 10;
player_view.y[5 * player_view[$size_of]] = 20;

create auto growing array of structs:

//creates array with 50 elements
const [player_view, player_arr] = ArrayStruct.create(player_meta, 50);

add elements with objects or per indicies (creation per indicies is around 3x faster: https://jsben.ch/7fhtA):

//fast
for(let i=0; i < 100; i++){
    let index = player_arr.add_one();
    player_view.hp[index] = 100;
    player_view.x[index] = 10;
    player_view.y[index] = 20;
}

//slow
for(let i=0; i < 100; i++){
    player_arr.push({
        hp: 100,
        x: 10,
        y: 20
    });
}

array-of-structs's People

Contributors

rehkitzdev avatar

Stargazers

 avatar Eric McDaniel avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.