Testing purescript functions in jsPerf

176 Views Asked by At

I would like to compare the performance of two PureScript functions in jsPerf.

What compilation do I need to do and what parts do I need to put in 'setup' and each snippet box?

Using psc or pulp.

1

There are 1 best solutions below

1
radrow On

Use FFI

Because you are referring to a JavaScript utility it would be the easiest to write the test in JS.

You can write your performance tests in a separate js file (named accordingly to your testing module name) and call it from purescript via Foreign Function Interface.

Assuming you want to compare performance of f and g functions, the code scheme could be described by the following template:

-- File PerfTest.purs
module PerfTest where

f :: Args -> Result
f args = ...

g :: Args -> Result
g args = ...

foreign import performanceTestImpl 
  :: forall a. (Unit -> a) -> (Unit -> a) -> Unit

main :: Effect Unit
main =
  pure $ performanceTestImpl (\_ -> f args) (\_ -> g args)
// File PerfTest.js
"use static";

exports.performanceTestImpl =
  function(runF) {
    return function(runG) {
      // run jsPerf tests as you would normally do
    };
  };

This will delegate the performanceTestImpl implementation into JavaScript with two callbacks whose performance should be measured and compared. Note that you need to pass unevaluated lambdas in order to defer the computation since PureScript isn't lazy as Haskell. PureScript should take care of linking. Note that both files need to have matching names and be put in the same directory.