Skip to content

Commit 5868203

Browse files
⚡ Optimize timestamp generation in BackgroundService (#224)
* Refactor BackgroundService to use Date.now() instead of moment() for timestamps Co-authored-by: xRahul <1639945+xRahul@users.noreply.github.com> * Fix linting issues in BackgroundService.js and benchmark_moment.js Co-authored-by: xRahul <1639945+xRahul@users.noreply.github.com> --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: xRahul <1639945+xRahul@users.noreply.github.com>
1 parent 541c42d commit 5868203

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

scripts/benchmark_moment.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const moment = require('moment');
2+
3+
const ITERATIONS = 1000000;
4+
5+
console.log(`Running benchmark with ${ITERATIONS} iterations...`);
6+
7+
// Benchmark Moment.js
8+
const startMoment = process.hrtime();
9+
for (let i = 0; i < ITERATIONS; i++) {
10+
moment()
11+
.valueOf()
12+
.toString();
13+
}
14+
const endMoment = process.hrtime(startMoment);
15+
const timeMoment = endMoment[0] * 1000 + endMoment[1] / 1000000;
16+
17+
// Benchmark Date.now()
18+
const startDate = process.hrtime();
19+
for (let i = 0; i < ITERATIONS; i++) {
20+
Date.now().toString();
21+
}
22+
const endDate = process.hrtime(startDate);
23+
const timeDate = endDate[0] * 1000 + endDate[1] / 1000000;
24+
25+
console.log(`Moment.js: ${timeMoment.toFixed(2)}ms`);
26+
console.log(`Date.now(): ${timeDate.toFixed(2)}ms`);
27+
console.log(`Improvement: ${(timeMoment / timeDate).toFixed(2)}x faster`);

src/services/BackgroundService.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import AsyncStorage from '@react-native-community/async-storage';
22
import BackgroundTimer from 'react-native-background-timer';
33
import PushNotification from 'react-native-push-notification';
4-
import moment from 'moment';
54
import {USER_AGENT_DESKTOP, WEB_PLATFORM_DESKTOP} from '../Constants';
65
import {escapeRegExp} from '../Utils';
76

@@ -55,12 +54,7 @@ export const checkUrlForText = async checkUrlForTextData => {
5554
});
5655
}
5756

58-
await AsyncStorage.setItem(
59-
'lastChecked',
60-
moment()
61-
.valueOf()
62-
.toString(),
63-
);
57+
await AsyncStorage.setItem('lastChecked', Date.now().toString());
6458
} catch (error) {
6559
console.log(error);
6660
}

0 commit comments

Comments
 (0)