javascript-toolbelt interactive documentation

The documentation is live and interactive powered by the KLIPSE plugin:

  1. Live: The code is executed in your browser
  2. Interactive: You can modify the code and it is evaluated as you type

Setup

First let's load JST from the javascript-toolbelt github repository (It might take a couple of seconds...):


  Object.keys(JST)

System Time

system_time returns highest resolution time offered by host in milliseconds.


 JST.system_time()  

Elapsed Time

time returns a string with the number of msec resolution time offered by host in milliseconds.


 JST.time(function(){
    Math.sqrt(1232);
 })  

Benchmark

benchmark returns a string with the number of msec to run a function n times.


 JST.benchmark(100, function(){
    Math.sqrt(1232);
 })  

Permutations

permutationsis a handy function that returns the permutations of an array.

Thank you StackOverflow!


JST.permutations([1,2,3])

And it is quite fast. You can judge by yourself:


JST.time(function() { return JST.permutations([1,2,3,4]);})

Memoization

memoize returns a memoized version of a 1-arity function.


memo = JST.memoize(JST.permutations);

[
    JST.time(function() {
        JST.permutations([1,2,3,4,5]);
    }),
    JST.time(function() {
        memo([1,2,3,4,5]);
    }),
    JST.time(function() {
        memo([1,2,3,4,5]);
    })
]

As you can see, the first call to memo takes about the same time as the direct call to JST.permutations. But subsequent calls are very fast.

minBy and maxBy

maxBy returns the element of the array that leads to the greatest value.

Thanks StackOverflow!


JST.maxBy(function(s) { return s.length;}, ['albatross', 'dog', 'horse'])

minBy returns the element of the array that leads to the greatest value.


JST.minBy(function(s) { return s.length;}, ['albatross', 'dog', 'horse'])


flattenObject

flattenObject returns an object with the keys flattened.


JST.flattenObject({
  x: 1,
  y: {z: 2, w: [3, 4]}
})

minBy returns the element of the array that leads to the greatest value.