Skip to content

Commit

Permalink
perf(rrule-set): do not use napi's iterator macro (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
lsndr committed Jun 22, 2024
1 parent 9e5aa25 commit 234d16d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
14 changes: 14 additions & 0 deletions benchmark/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ function suite(tzid: string) {

set.all();
}),
b.add('...RRuleSet (rust)', () => {
const rrule = new rust.RRule({
frequency: rust.Frequency.Daily,
count: 30,
interval: 1,
});
const set = new rust.RRuleSet({
dtstart: rust.DateTime.create(2023, 2, 21, 23, 59, 0, false),
tzid,
rrules: [rrule],
});

[...set];
}),
b.add('RRule.all() (node)', () => {
const rrule = new node.RRule({
freq: node.RRule.DAILY,
Expand Down
12 changes: 4 additions & 8 deletions lib/js/rrule_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::rrule::exdate::ExDate;
use crate::rrule::rdate::RDate;
use crate::rrule::{rrule, rrule_set};
use napi::bindgen_prelude::{Array, Reference, SharedReference};
use napi::iterator::Generator;
use napi::Env;
use napi_derive::napi;
use replace_with::replace_with_or_abort_and_return;
Expand Down Expand Up @@ -286,18 +285,15 @@ impl RRuleSetIterator {
}
}

#[napi(iterator)]
#[napi]
pub struct RRuleSetIteratorIterable {
iterator: SharedReference<RRuleSetIterator, rrule_set::RRuleSetIteratorIterable<'static>>,
}

#[napi]
impl Generator for RRuleSetIteratorIterable {
type Yield = i64;
type Next = ();
type Return = ();

fn next(&mut self, _next: Option<Self::Next>) -> Option<Self::Yield> {
impl RRuleSetIteratorIterable {
#[napi]
pub fn next(&mut self) -> Option<i64> {
self.iterator.next().map(|date: DateTime| (&date).into())
}
}
2 changes: 1 addition & 1 deletion src/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@ export class RRuleSetIterator {
iterator(): RRuleSetIteratorIterable
}
export class RRuleSetIteratorIterable {
[Symbol.iterator](): Iterator<number, void, void>
next(): number | null
}
11 changes: 7 additions & 4 deletions src/rrule-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,22 @@ export class RRuleSet implements Iterable<DateTime> {
}

public [Symbol.iterator]() {
const iter = this.toRust().iterator().iterator()[Symbol.iterator]();
const iter = this.toRust().iterator().iterator();

return {
next: () => {
const result = iter.next();

if (result.done) {
return result;
if (result === null) {
return {
done: true as const,
value: undefined,
};
}

return {
done: false,
value: DateTime.fromNumeric(result.value),
value: DateTime.fromNumeric(result),
};
},
};
Expand Down

0 comments on commit 234d16d

Please sign in to comment.