brain.js - Neural networks in JavaScript

ben profile image Ben Halpern ・1 min read

This is a fairly remarkable project which offers a library of Neural Networks written in JavaScript.

GitHub logo BrainJS / brain.js

πŸ€– Neural networks in JavaScript



GPU accelerated Neural networks in JavaScript for Browsers and Node.js

npm js-standard-style Backers on Open Collective Sponsors on Open Collective Gitter Slack Build Status



brain.js is a GPU accelerated library for Neural Networks written in JavaScript.

πŸ’‘ This is a continuation of the harthur/brain, which is not maintained anymore. More info

Table of Contents

Installation and Usage


If you can install brain.js with npm:

npm install brain.js


<script src="//unpkg.com/brain.js"></script>


Download the latest brain.js for browser

Installation note


Here's an example showcasing how to approximate the XOR function from the README:

// provide optional config object (or undefined). Defaults shown.
const config = {
    binaryThresh: 0.5,
    hiddenLayers: [3],     // array of ints for the sizes of the hidden layers in the network
    activation: 'sigmoid'  // supported activation types: ['sigmoid', 'relu', 'leaky-relu', 'tanh'],
    leakyReluAlpha: 0.01   // supported for activation type 'leaky-relu'

// create a simple feed forward neural network with backpropagation
const net = new brain.NeuralNetwork(config);

net.train([{input: [0, 0], output: [0]},
           {input: [0, 1], output: [1]},
           {input: [1, 0], output: [1]},
           {input: [1, 1], output: [0]}]);

const output = net.run([1, 0]);  // [0.987]

It's actively maintained. Definitely worth checking out.

This post is part of the new GitHunt DEV tag. Follow it out for more like this.


Interesting open source repos you've discovered and want to share with the community.

Posted on by:

ben profile

Ben Halpern


A Canadian software developer who thinks he’s funny. He/Him.


Editor guide

I love the GitHunt tag and the projects you've been posting, Ben. This has been on my list of libraries I need to play with for a long time. I like Martin's spam filter project idea; anyone else have any other projects or ideas for what to do with this that I can draw inspiration from?


I love this one. Used it a while back in a side project to filter spammy chat messages and it worked like a charm.

It's also one of the few JS ML libraries that are actively maintained.

A big "competitor" to brain.js is TensorFlow.js, which is backed by Google and Tensorflow in general is very widely used. It was exciting to see Tensorflow coming to JavaScript this year.


Great to hear you had success on spam filtering. That's a notoriously difficult problem without tech like this, but even with neural networks, I don't hear a lot of practical "this worked and how" examples.

If you wanted to write a DEV post on that experience, I think it would go over really well.


I built a sentence matching system using this library. It matched the words and characters in an input sentence against a database to make a best guess match. The standardised sentence version in the database was linked to properties we used for processing a response.
The response was a set of calculations rather than a chat app.
The library was useful in 3 ways:

  1. We could claim to use AI in the app πŸ˜€.
  2. Once trained (it runs in node but could have done it in browser) you save the settings for fast and repeated use. I think, but never tested this, that it is faster than a traditional brute force best match search.
  3. You don't really need to know much about neural networks to set it up. Getting your training and testing data organised is by far the hardest part.

does the speed of computation comparable with c tensorflow library?


Wow, looks great, thanks for sharing! I'll see if I can try it out soon!


Love this. Congrats!


If you're running in NodeJS anyway, I'd expect you're better of with some kind of wrapper around native TensorFlow bindings than running a neural network in pure JS.