Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RXJS - simple if vs filter
(version: 0)
Comparing performance of:
Simple if vs Filter
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://unpkg.com/rxjs@^7/dist/bundles/rxjs.umd.min.js"></script>
Tests:
Simple if
const obs$ = rxjs.of({name: 'Test'}); obs$.pipe( rxjs.tap((user) => { if (user.name) { console.log(user.name); } })).subscribe();
Filter
const obs$ = rxjs.of({name: 'Test'}); obs$.pipe( rxjs.filter((user) => user.name), rxjs.tap((user) => { console.log(user.name); })).subscribe();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Simple if
Filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Edg/139.0.0.0
Browser/OS:
Chrome 139 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Simple if
462271.1 Ops/sec
Filter
436407.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches for filtering an observable in RxJS: `tap` and `filter`. The goal is to measure the performance difference between these two methods on a simple observable creation. **Script Preparation Code** The script preparation code includes the inclusion of the RxJS library via a CDN link (`https://unpkg.com/rxjs@^7/dist/bundles/rxjs.umd.min.js`). This allows users to create an observable and perform operations on it using RxJS functions. **Individual Test Cases** There are two test cases: 1. **Simple if**: This test case creates an observable with a single element (`{name: 'Test'}`) and uses the `tap` function to log the `name` property of each element. The `tap` function is used instead of filtering the elements. 2. **Filter**: This test case creates an observable with a single element and uses both `filter` and `tap` functions. The `filter` function filters out elements that don't have a `name` property, and then the `tap` function logs the `name` property of the remaining elements. **Pros and Cons** * **Tap**: Pros: + Simple to implement + Can be used for side effects or logging Cons: + Doesn't filter out elements; it only logs those that pass a condition + May not be efficient if you need to process all elements * **Filter**: Pros: + Efficiently filters out unwanted elements + Allows for more complex filtering logic Cons: + More verbose than `tap` + May require additional processing steps after filtering **Other Considerations** When choosing between `tap` and `filter`, consider the following: * Do you need to process all elements or can you skip some? * Are there side effects that need to be performed on each element (e.g., logging)? * Is efficiency critical, or do you prioritize simplicity? **Library: RxJS** RxJS is a reactive programming library for JavaScript. It provides a set of operators and functions for working with observables, which are a fundamental concept in functional programming. In this benchmark, RxJS is used to create an observable and perform operations on it. **Special JS Features/Syntax (not applicable)** There are no special JavaScript features or syntaxes used in this benchmark that require explanation. **Alternatives** If you need to filter observables in RxJS, you can also use other operators like `switchMap` or `mergeMap`. However, the `filter` and `tap` operators are often the most straightforward choices for simple filtering needs.
Related benchmarks:
compat vs filter
Filter Comparison
_.compact vs filter(Boolean) vs filter(element => element)
filter vs if
filter(_ => _) vs filter(Boolean)
Comments
Confirm delete:
Do you really want to delete benchmark?