GithubHelp home page GithubHelp logo

code-golf's Introduction

code-golf

My code golf solutions

JavaScript Code Golf Notes

Operators

  • remove whitespace after operators when possible
  • bitwise OR |
    • number|0 equivalent to Math.trunc(number)
  • bitwise NOT ~
    • sets number to -(number+1), so can be used with functions that return -1 if a 0 return is needed
    • coerces non-numbers to -1
    • use with search() (though match() should be used over search())
  • bitwise XOR ^
    • replace != with ^ when comparing integers
  • bracket operator []
    Object[m='method'](arguments) // first method/property call
    Object[m](arguments)          // subsequent calls
length of method/property minimum calls
3 7
4 4
5 3
6 3
7 3
8+ 2
  • comma ,
    • replace curly braces in conditionals and loops with commas between statements
  • power **
    • a**b is equivalent to Math.pow(a,b)
  • spread ...
    • saves bytes when no callback is needed

Loops

  • for, for in , for of
    • reverse loops to remove expressions
      for (a=0;a<b.length;a++) // before
      for (a=b.length;a--;)    // after
    • [...array] use spread operator when possible

Methods

  • template literals instead of parentheses when possible
  • isFinite()
    • 1/number to check instead
  • isNaN()
    • number^number instead of isNaN(number)
  • Math.pow()
    • ** operator
  • Math.random()
    • new Date & 1 == Math.random() < .5
    • new Date % 330 == Math.floor(Math.random()*330)
  • Math.trunc(), Math.round()
    • |0 bitwise OR instead
  • RegExp()
    • eval() instead
  • array
    • use coercion ''+array instead of join(',') or join()
    • spread operator instead of forEach() when possible
  • string
    • match() instead of search() instead of indexOf() unless regex is longer than string or you need a numeric return
    • slice() is always shorter than substring, can slice(-n) to get the last n elements
    • split() use spread operator when splitting over every element

Variables

  • use type specific methods to test type
type test
array pop()
function call()
number toFixed()
string big()
textNode data()
  • initialize variables in parameters of functions or loops
  • this or self instead of window
  • array
    • map() instead of reduce() when possible
    • use elision to put values in certain indices
  • boolean
    • !0 for true and !1 for false if you need literal boolean values, or else use 1 and 0
    • !! to change a value to true or ! for false
  • function
    • omit parentheses on new calls without arguments
  • Infinity
    • 1/0 instead of Infinity
  • number
    • +number instead of parsing to coerce to float or integer
    • AeB instead of numbers with many trailing zeros
    • number|0 to cast to integer, equivalent to Math.trunc(number)

Regex

  • \b for word boundaries
  • \d instead of [0-9]
  • \w instead of [A-Za-z0-9_]

code-golf's People

Contributors

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