Skip to content

Commit ac57f23

Browse files
committed
Use setImmediate where available
Using setImmediate massively improves performance. Since most browsers don't support it, you'll have to include a shim for it, as we don't provide any.
1 parent 4ef83a3 commit ac57f23

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

README.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ You can install it via a package manager:
3030
or [download source code](https://github.com/dchest/scrypt-async-js/releases).
3131

3232

33+
To improve performance with small interruptStep values, use `setImmediate` shim,
34+
such as <https://github.com/YuzuJS/setImmediate>.
35+
36+
3337
Usage
3438
-----
3539

@@ -38,10 +42,10 @@ Usage
3842
Derives a key from password and salt and calls callback
3943
with derived key as the only argument.
4044

41-
Calculations are interrupted with zero setTimeout at the given
42-
interruptSteps to avoid freezing the browser. If interruptStep is not given,
43-
it defaults to 1000. If it's zero, the callback is called immediately after
44-
calculation, avoiding setTimeout.
45+
Calculations are interrupted with setImmediate (or zero setTimeout) at the
46+
given interruptSteps to avoid freezing the browser. If interruptStep is not
47+
given, it defaults to 1000. If it's zero, the callback is called immediately
48+
after the calculation, avoiding setImmediate.
4549

4650
#### Arguments:
4751

scrypt-async.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
* Derives a key from password and salt and calls callback
1515
* with derived key as the only argument.
1616
*
17-
* Calculations are interrupted with zero setTimeout at the given
18-
* interruptSteps to avoid freezing the browser. If interruptStep is not given,
19-
* it defaults to 1000. If it's zero, the callback is called immediately after
20-
* calculation, avoiding setTimeout.
17+
* Calculations are interrupted with setImmediate (or zero setTimeout) at the
18+
* given interruptSteps to avoid freezing the browser. If interruptStep is not
19+
* given, it defaults to 1000. If it's zero, the callback is called immediately
20+
* after the calculation, avoiding setImmediate.
2121
*
2222
* @param {string|Array.<number>} password Password.
2323
* @param {string|Array.<number>} salt Salt.
@@ -420,16 +420,18 @@ function scrypt(password, salt, logN, r, dkLen, interruptStep, callback, encodin
420420
}
421421
}
422422

423+
var nextTick = (typeof setImmediate !== 'undefined') ? setImmediate : setTimeout;
424+
423425
function interruptedFor(start, end, step, fn, donefn) {
424426
(function performStep() {
425-
setTimeout(function() {
427+
nextTick(function() {
426428
fn(start, start + step < end ? start + step : end);
427429
start += step;
428430
if (start < end)
429431
performStep();
430432
else
431433
donefn();
432-
}, 0);
434+
});
433435
})();
434436
}
435437

scrypt-async.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)