Skip to content

Commit 6b350d7

Browse files
committed
Merge pull request #18 from dchest/setImmediate
Use setImmediate where available
2 parents ce29583 + 1960961 commit 6b350d7

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
sudo: false
12
language: node_js
23
node_js:
34
- "stable"

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)