Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

code in the FAQ doesn't seem to work w.r.t. setup #87

Closed
simonsarris opened this Issue Jun 24, 2012 · 4 comments

Comments

Projects
None yet
3 participants

http://jsperf.com/faq#setup-teardown

Using ui.benchmarks[0].setup seems to generate an error when used in a test, for example:

http://jsperf.com/content-display-strategies

Owner

mathiasbynens commented Jun 25, 2012

Why are you using it here? Unless I’m missing something, you could simply use the “setup” form field in this case. What are you trying to do?

Preparation code runs before the tests are defined, hence the error.

/cc @jdalton

Collaborator

jdalton commented Jun 26, 2012

@simonsarris Ya, you are right. We should tweak the FAQ entry to wrap it in a setTimeout(...).

Owner

mathiasbynens commented Jun 26, 2012

Wouldn’t it be better to update the FAQ saying that the “setup” and “teardown” fields can now be used for this? The FAQ entry was written back when there was only a “setup” field, which simply got injected in the global scope.

Collaborator

jdalton commented Jun 26, 2012

@mathiasbynens The snippet mentions "setup" and "teardown" fields.

Is it possible to execute code before and after each clocked test loop, outside of the timed code region?

That’s what Benchmark#setup and Benchmark#teardown are for. You can use the Setup and Teardown fields to use the same function(s) for all tests. To target specific tests, you can use ui.benchmarks[0].setup = function(){ … }; (and/or teardown) in the Preparation Code field, where 0 is the zero-indexed test ID.

could be

Is it possible to execute code before and after each clocked test loop, outside of the timed code region?

That’s what Benchmark#setup and Benchmark#teardown are for. You can use the Setup and Teardown fields to use the same function(s) for all tests. To target specific tests, you can use setTimeout(function() { ui.benchmarks[0].setup = function(){ … }; }, 1) (and/or teardown) in the Preparation Code field, where 0 is the zero-indexed test ID.

@jdalton jdalton added documentation and removed bug labels May 14, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment