sanctuary-js / sanctuary-pair Goto Github PK
View Code? Open in Web Editor NEW:rainbow: Fantasy Land -compliant Pair type
License: MIT License
:rainbow: Fantasy Land -compliant Pair type
License: MIT License
I had an issue with a super simple parser that took seconds to parse simple objects. After profiling, I found the bottleneck to be Pair
. Changing from Pair
to an Array
Tuple made my use-case go from seconds to 40ms!
I made a reduced test case here: https://dotnetcarpenter.github.io/common-scandi-names/performance.html
All tests and profiles I have made are with type checking disabled.
Using S.Pair
is at least 80% slower than using an Array 2-tuplet, in this simple parsing test using S.reduce
. Where tokens
are https://gist.github.com/dotnetCarpenter/a52ff00f8d9a1cf1fcff23114a0fabea#file-norwegian-tokens-js.
const TOKEN = Object.freeze ({
PARENT: 8,
CHILD: 16,
})
// Token = { value: Any, type: Number }
// parse :: Pair Ast (Array a) -> Token -> Pair Ast (Array a)
const parse = pair => token => {
let ast = S.fst (pair), parent = S.snd (pair)
switch (token.type) {
case TOKEN.PARENT:
parent = ast[token.value] = []
break
case TOKEN.CHILD:
parent.push (token.value)
break
default:
// skip token
}
return S.Pair (ast) (parent)
}
// parser :: Array Token -> Ast
const parser = S.pipe ([
timeStart ('parse'),
S.reduce (parse) (S.Pair ({}) ([])),
S.fst,
timeEnd ('parse'),
])
parser (tokens)
You should be able to copy/paste the code from this gist into nodejs and see the difference (the two data sets are also in the same gist.
Even with the mangled files names due to rollup/vite bundling, it is still easy to spot where the majority of time is spent.
function Pair(fst) {
return snd => {
const pair = Object.create (prototype);
if (Z.Setoid.test (fst) && Z.Setoid.test (snd)) {
pair['fantasy-land/equals'] = Pair$prototype$equals;
if (Z.Ord.test (fst) && Z.Ord.test (snd)) {
pair['fantasy-land/lte'] = Pair$prototype$lte;
}
}
if (Z.Semigroup.test (fst)) {
if (Z.Semigroup.test (snd)) {
pair['fantasy-land/concat'] = Pair$prototype$concat;
}
pair['fantasy-land/ap'] = Pair$prototype$ap;
pair['fantasy-land/chain'] = Pair$prototype$chain;
}
pair.fst = fst;
pair.snd = snd;
return pair;
};
}
Pair
does rigorous testing regardless of type checking mode, in order to know which ADT's to implement. A fix would need to make sure that these tests will still function.
This is how'd I'd expect to do it, but $.Pair
does not exist...
S.is($.Pair ($.Any) ($.Any) )
@gabejohnson has expressed interest in working on this. :)
$ bower register sanctuary-tuples git://github.com/sanctuary-js/sanctuary-tuples.git
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.