Test network latency (ping) using an image request.
| Argument |
Description |
Type |
Promise<number> |
Promise that resolves to request duration (milliseconds) |
Promise |
| Parameter |
Description |
Type |
Default |
url |
Image URL (optional, defaults to GitHub favicon) |
string |
Optional |
import { imageRequest } from 'ranuts';
const latency = await imageRequest();
console.log('Network latency:', latency, 'ms');
import { imageRequest } from 'ranuts';
const latency = await imageRequest('https://example.com/test-image.jpg');
console.log('Latency:', latency, 'ms');
import { imageRequest } from 'ranuts';
async function testNetwork() {
try {
const latency = await imageRequest();
if (latency < 100) {
console.log('Good network');
} else if (latency < 300) {
console.log('Average network');
} else {
console.log('Slow network');
}
} catch (error) {
console.error('Test failed:', error);
}
}
- Default URL: If URL is not provided, defaults to GitHub's favicon (about 2.2KB).
- Measurement method: Measures network latency by timing image load, from when the request starts to when the image finishes loading.
- Error handling: If image loading fails, Promise will reject.
- Use case: Commonly used for network quality detection, performance monitoring, etc.