Check if an
Iterable
is empty
More efficient than Array.from(iterable).length
, as it only iterates the first item, which can make a big difference for dynamic iterators, like a Generator
doing an expensive operation.
npm install is-empty-iterable
import isEmptyIterable from 'is-empty-iterable';
isEmptyIterable([]);
//=> true
isEmptyIterable(new Set([1, 2]));
//=> false
Returns a boolean
.
Type: Iterable
When using with generators, be aware that generators are consumed after being iterated. This means calling isEmptyIterable()
on the same generator instance multiple times will return different results:
function* myGenerator() {
yield 1;
yield 2;
}
const generator = myGenerator();
isEmptyIterable(generator);
//=> false
isEmptyIterable(generator); // Generator is now consumed
//=> true
If you need to check the same generator multiple times, create a new generator instance for each check:
function* myGenerator() {
yield 1;
yield 2;
}
isEmptyIterable(myGenerator());
//=> false
isEmptyIterable(myGenerator());
//=> false