• Stars
    star
    174
  • Rank 211,968 (Top 5 %)
  • Language
    HTML
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Highly performant Javascript library to flip the signs

GG Flip

Donald Knuth described in one of his TAOCP books that flipping the sign of a number is one of the hardest problems in Computer Science. But that was in the 60s. Thanks to years of research and after numerous publications, today have some interesting ways to do the same. Consider this:

x = 5
// get -5
x -= x*2;

Above is one of the simplest and most efficient ways to flip the sign of a number. It uses the 'minimal' approach and there is now a popular npm library based on it. It actually works and is highly performant:

performance-report

But as you can see, in today's webscale world flipping ~4000 signs per second wouldn't cut it. I started to look into the ways to improve it and started reading the code of V8 engine. My C is rusty, but I was able to read through most of the code and understand it. Turns out V8 highly optimises the switch blocks and they are faster compared to any other control statements. I decided to make use of this and implement a better sign flip. GG Flip is the result.

GG Flip is a Golang library which generates the Javascript sign flip library. I preferred Go because lack of generics seemed like a good design choice. This code has no external dependencies, you can run:

git clone https://github.com/avinassh/gg-flip.git
cd gg-flip
go run main.go

Above code generates highly readable file lib.js, which is:

function signFlip(num) {
    switch (num) {
        case 0:
            return -0;
        case -0:
            return 0;
        case 1:
            return -1;
        case -1:
            return 1;
        // ...
        case 9:
            return -9;        
        case -9:
            return 9;
        // ...
        case 9007199254740991:
            return -9007199254740991;
        case -9007199254740991:
            return 9007199254740991;
        default:
            return num-num*2;
    }
}

Thanks to V8 and their clever switch block optimizations, the above code becomes highly performant. If the jumps in switch take a long time, then it fallbacks to the default, which flips the sign using minimal approach. It's a win-win! See the performance by yourself:

performance-report2

Even in low memory environments (old computers, internet explorer, iPhones with low battery), GG Flip performs almost twice as fast compared to the minimal approach and in the usual environments, it's almost 10x faster than the usual method and 4x faster than the minimal approach. Currently, GG Flip works only with the integers and for floats, it fallbacks to default switch case. In the next version, I will include all the float numbers, so that for floats also the performance remains same.

The final lib size is few GBs, so distribution may become a problem. But I am talking with popular CDN providers like Google, Cloudflare etc to ship this library directly. I am planning to send a PR to Javascript with this library so that GG Flip comes as a standard library as part of the glorious language.

I am also exploring Blockchain to see if this library can be distributed securely and quickly, to everyone. Bitcoin Cash which is a fork of Bitcoin, uses 4MB per blocks and hence might be a good candidate to distribute the GG Flip.

GG Flip as a Service

I am now providing GG Flip as a web service, check - GaS. Now, with a simple API request, you can flip the sign of numbers and get all the benefits. GaS is hosted on Cloud and hence is very fast.

License

The mighty MIT license. Please check LICENSE for more details.

More Repositories

1

rockstar

Makes you a Rockstar C++ Programmer in 2 minutes
Python
4,147
star
2

py-caskdb

(educational) build your own disk based KV store
Python
968
star
3

grpc-errors

A handy guide to gRPC errors
C#
549
star
4

fast-sqlite3-inserts

Some bunch of test scripts to generate a SQLite DB with 1B rows in fastest possible way
Rust
337
star
5

status

HTTP Status for Humans
Python
276
star
6

pytorch-flask-api

Python
166
star
7

pytorch-flask-api-heroku

HTML
160
star
8

haxor

Unofficial Python wrapper for official Hacker News API
Python
153
star
9

go-caskdb

(educational) build your own disk based KV store in Go
Go
134
star
10

slackipy

Automate user invites to your Slack channel!
Python
87
star
11

della

Della is a Django app for managing Secret Santa/Gift Exchange.
Python
48
star
12

Reddit-GoodReads-Bot

Python
32
star
13

little-finger

Clojure
31
star
14

cowin-assist

Telegram Bot to check covid vaccine slot availability on CoWin site
Python
30
star
15

prawoauth2

Helper library to make your life easier using OAuth2 for PRAW
Python
29
star
16

kylo

The FAQ Bot
Python
16
star
17

history-bleed

Check your browser history against Cloud Bleed
JavaScript
13
star
18

score-notify

Displays cricket score as notification. OS X Only.
Python
11
star
19

dev-startups-india

list of Indian startups working on building developer tools
9
star
20

fluvio-go

Rust
8
star
21

saveourcinema

code which powers http://saveourcinema.in
HTML
8
star
22

ares

Slack Moderator
Go
8
star
23

Hocus-Pocus

Simple OS X app which shows hidden files. Written in Swift.
Swift
8
star
24

breast-cancer-prediction

Jupyter Notebook
7
star
25

openshift-tornado-starter

Simple repo to get started on Openshift with Python Tornado
6
star
26

cemetery.io

HTML
5
star
27

sign-flip

JavaScript
5
star
28

isso-openshift

This repo helps you install Isso on Openshift with a single click
Python
5
star
29

brave-browser-hardening

mirror of https://gitlab.com/CHEF-KOCH/brave-browser-hardening
5
star
30

Laozi

Laozi is a Goodreads bot for Telegram
Python
5
star
31

heroku-tornado-starter

Simple repo to get started on Heroku to serve static files using Python Tornado
Python
5
star
32

nightreads

Python
5
star
33

slackipycore

Invite Users to Slack using Python
Python
4
star
34

talkwithme

simple chatroom built using Tornado
Python
4
star
35

unnamed-wip-document-db

a document database (like mongo) written in Python. Uses CaskDB as storage backend.
4
star
36

learning-scraping

A project written in Django, which aims to teach Web Scraping for beginners.
Python
3
star
37

polistats

Google Search Trends of Indian Politics
Python
3
star
38

kekday

Find a Redditor's Cake Day!
HTML
3
star
39

good-human

Python
2
star
40

gh-vue

A simple Vue JS project to display my latest commits from Github
JavaScript
2
star
41

learning-tornado

Python
2
star
42

btree

An in-memory B Tree implementation in Go
Go
2
star
43

tildes-plus

JavaScript
2
star
44

mca_scraper

Python
2
star
45

gofish

Go
2
star
46

little-finger-android

Java
2
star
47

hugo-skyfall

Hugo theme - skyfall
CSS
2
star
48

mvcc-go

Go
2
star
49

reddit-india-books

What does Reddit India likes to read?
Python
2
star
50

skyfall

Clean Pelican theme for http://avi.im/blag
HTML
2
star
51

mkstemp

A Hacker News Clone
Python
2
star
52

offenders

List of Telecos and Companies which break Net Neutrality
2
star
53

Bitbucket-import

Python
1
star
54

monkey

An interpreter for the Monkey Programming Language in Golang
Go
1
star
55

wutong

A naughty Telegram bot
Python
1
star
56

tornado-benchmarks

pointless tornado benchmarks
Python
1
star
57

FoodPin

Swift
1
star
58

rtiman

Kickstarter for RTIs
Python
1
star
59

avinassh.github.io

HTML
1
star
60

bast

Delete your Reddit comment history safely
Go
1
star
61

blag

hosts my blog
CSS
1
star
62

goodreads-export

CSS
1
star
63

little-finger-ios

Swift
1
star
64

inks

Go
1
star
65

Calculator-Swift

Simple Calculator app written using Swift
Swift
1
star
66

honk

Go
1
star
67

ratelimit

Go
1
star
68

heroku-static

Ruby
1
star
69

build-your-regex

Go port of Rob Pike's Regex Engine (from C)
Go
1
star
70

hugo-publish

My Github Actions setup for publishing a Hugo blog
1
star