Skip to content

Commit

Permalink
Added Debug.logTime function
Browse files Browse the repository at this point in the history
  • Loading branch information
albertdahlin committed Dec 1, 2018
1 parent 22b2d7a commit 23a429c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/Debug.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ module Debug exposing
( toString
, log
, todo
, logTime
)

{-| This module can be useful while _developing_ an application. It is not
available for use in packages or production.
# Debugging
@docs toString, log, todo
@docs toString, log, todo, logTime
-}


Expand Down Expand Up @@ -95,3 +96,32 @@ todo : String -> a
todo =
Elm.Kernel.Debug.todo


{-| Log the execution time of a function.
This will help you track down performance issues by logging the execution
time of any function to the console. Most browsers also displays the time in
their [performance debugging] tools.
The first argument is a label which will be displayed in the console. This helps
when tracking multiple functions at the same time.
This will call `fn` with `arg` and log the execution time in the console. The
return value will be whatever `fn` returns.
logTime "name in console" fn arg
If a function takes more than one argument you need to apply all but the last
one, effectively creating a one argument function.
logTime "render header" (renderHeader arg1 arg2) arg3
**Note:** This is not available with `elm make --optimize` or in packages. The
idea is that `logTime` can be useful during development, but you would't want
to clutter the console with this information in the resulting application.
[performance debugging]: https://developers.google.com/web/tools/chrome-devtools/console/track-executions
-}
logTime : String -> (a -> b) -> a -> b
logTime =
Elm.Kernel.Debug.logTime

16 changes: 16 additions & 0 deletions src/Elm/Kernel/Debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,19 @@ function _Debug_regionToString(region)
}
return 'on lines ' + region.__$start.__$line + ' through ' + region.__$end.__$line;
}

// LOG TIME

var _Debug_logTime__PROD = F3(function(name, fn, arg)
{
return fn(arg);
});

var _Debug_logTime__DEBUG = F3(function(name, fn, arg)
{
console.time(name);
var returnValue = fn(arg);
console.timeEnd(name);

return returnValue;
});

0 comments on commit 23a429c

Please sign in to comment.