Code Monkey home page Code Monkey logo

grid-benchmark.js's Introduction

I benchmarked 8 different ways of representing a 2d grid in JavaScript

Motivation: Find the fastest way for representing a 2d collision bitmap for a fast-paced JavaScript game (LineRage).

There are many ways to represent a bitmap-style grid in Javascript. I compiled a collection of implementations and benchmarked them.

  • Some were obvious but I thought were inefficient. Array full of arrays (grid_array_2d) turned out is the fastest overall despite having to create that many more objects.
  • Some I suspected would be most efficient. grid_array_1d turned out to be mediocre due to the extra math required for determining the 2d -> 1d index mapping.
  • Some I had no idea how they would compare. The hash-based structs almost compete with grid_array_2d, and might actually be better for sparse grids.
  • Some I was hoping would yield surprising results, like grid_canvas which was completely disappointing.

Needless to say, the results were very surprising to me.

If there are any corrections or additions, please fork and request a pull. Excuse the ghetto benchmarking code, I could not find a good standard benchmarking library that did what I needed.

Update: Anyone want to port the benchmarks to Benchmark.js? (Issue 1)

Results

YMMV, but I found generally the same trends across machines and browsers. These particular results are from my Macbook Pro on Chrome 8.0.552.231.

size = [1200,800]

grid_array_1d

create: 0ms
write: 154ms
read: 39ms
total: 194ms

grid_array_1d_bitwise

create: 1ms
write: 89ms
read: 65ms
total: 156ms

grid_array_2d

create: 12ms
write: 48ms
read: 38ms
total: 101ms

grid_array_2d_lazy

create: 6ms
write: 78ms
read: 38ms
total: 123ms

grid_canvas

create: 2ms
write: 1702ms
read: 4727ms
total: 6433ms

grid_hash

create: 177ms
write: 55ms
read: 35ms
total: 270ms

grid_hash_2d

create: 25ms
write: 50ms
read: 39ms
total: 116ms

grid_hash_lazy_2d

create: 0ms
write: 86ms
read: 45ms
total: 134ms

grid-benchmark.js's People

Contributors

dmcinnes avatar shazow avatar

Stargazers

 avatar  avatar

Watchers

 avatar  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.