Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash vs. Set Intersection vs Array intersection 3
(version: 2)
Comparing performance of:
Javascript Set intersection vs Lodash intersection vs Array intersection 1 vs Array intersection 2
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.19/lodash.min.js"></script>
Script Preparation code:
var first = ['# of cohort exports','# of Merged Events & Properties','3-month Pricing Plan Experiment Group','[Community] User Email','[Community] User Name','[Community] User Primary Role','[Community] User Rank','[Lexicon]# of Events','[Lexicon]# of Merged Events & Properties','[Lexicon]# of User Properties','[Not3] Last Notification Analytics View','[Not3] Last Priority Update','$sales_region','$tech_segment','a','AAAAAAAAQuickBrownFox','accept_invite_path','Account ID','Account Owner','Account Owner Role','Address','AFEWFWAEFse','age','$android_app_version','$android_brand','$android_devices','asd','asdfasd','asdfasdfasf','$avatar','behavioral prop','Beta Groups','Beta: Help with innovation phase?','Beta: Sign up date','Project owner billing account ID','Birthday','Blog Subscribe Date','$bounce_category','$braze_device_id','$braze_external_id','$browser','Cancelled','$city','$collections','Company','company','Company size','Completed onboarding intro version','Contact Sales Request Date','Content Download Date','$country_code','$created','CSM','Dashboard Whitelisted Project Owner','Domain','$email','encoded_email','Events plan id','Events plan name','Events plan price','events_plan_term_length_in_months','$first_name','fkwjeabsdkaj','foo','foozz','$geo_source','Get Demo Form Submit Date','$group_id','Has "My Dashboard"','Houzz Cohort','In-app Targeting','inviter_email','inviter_name','$ios_device_model','$ios_devices','Is lead','is paid','is_invited_user','id','$last_login','$last_name','Last Purchase','$last_seen','Lifetime Revenue','LinkedIn Handle','$marked_spam','$campaigns','Mixpanel Employee','moinak_test?','Most Active User','$mp_inapp_campaigns','my_special_prop','Name','$name','nesting','new_prop','new_prop_1','new_prop_2','new_prop_3','Num paid events plans','number','one','$os','Opt-in Status','opt_cookies','opt_email','opt_tracking','Organization ID','Organization Name','Origin','Owner','Partner Request Date','People plan price','Phone','$phone','Platforms integrated','Platforms integrated updated','$predict_grade','$predict_grade - Cohorts viewed report','$predict_grade - funnel','$predict_grade - message sent','$predict_grade - New signup predictions','$predict_grade - steve-cohort-test','$predict_grade - To be Signed up users','project_id','Project Name','QBQ Email Mismatch','Random Number','recoverd','recovered','$region','SDKs integrated','Seedlist_PK','SFDC Company','SFDC Last Activity Date','SFDC Owner Email','SFDC Owner Name','SFDC Relationship with Mixpanel','SFDC Role (ClearBit)','SFDC Role Type','SFDC Seniority (ClearBit)','SFDC Title','shold_not_see_this','Sign up date','Tab Views: Entity-management','Tab Views: Experiment-reporting','Tab Views: Funnels','Tab Views: Impact','Tab Views: Journeys','Tab Views: Launch-analysis','Tab Views: Lexicon','Tab Views: Profile','Tab Views: Segmentation','Tab Views: Stickiness','Tab Views: Transformations','Tab Views: Views','Tab Views: Visual-journeys','Tab Views: Workspaces','$team','Team','test3','three','Top_articles','$ae_total_app_sessions','two','$unsubscribed','User name','$username','USER_ID','utm_campaign [last touch]','utm_content [last touch]','utm_medium [last touch]','utm_source [last touch]','utm_term [last touch]','Webinar Form Submit Date','Work Location']; var second = ["$name", "$last_seen", "$country_code", "$region", "$city", "Account: ARR - Services", "Account: ARR - Total", "Account: CSM", "Account: current events tally", "Account: current MTU tally", "Account: current people tally", "Account: events/MTU plan ARR"];
Tests:
Javascript Set intersection
const firstSet = new Set(first); const secondSet = new Set(second); new Set([...firstSet].filter(item => secondSet.has(item)));
Lodash intersection
_.intersection(first, second)
Array intersection 1
first.filter((f) => second.includes(f))
Array intersection 2
second.filter((f) => first.includes(f))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Javascript Set intersection
Lodash intersection
Array intersection 1
Array intersection 2
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring performance differences in JavaScript is crucial for optimizing code efficiency, especially when dealing with large datasets or complex operations. The provided benchmark definition measures the execution time of three approaches to find the intersection between two arrays: `Set` intersection (using native JavaScript's `Set` object), Lodash's `intersection` function, and array intersection using `.filter()` method. We'll dive into each approach and discuss their pros and cons: ### 1. Native JavaScript Set Intersection ```javascript const firstSet = new Set(first); const secondSet = new Set(second); new Set([...firstSet].filter(item => secondSet.has(item))); ``` * **Pros:** * Fastest execution time due to the use of native `Set` object operations. * Least memory usage compared to other approaches. * **Cons:** * May not be supported in older browsers or environments with outdated JavaScript implementations. ### 2. Lodash Intersection ```javascript _.intersection(first, second); ``` * **Pros:** * Widely supported across different browsers and environments due to its reliance on the popular Lodash library. * Can leverage caching mechanisms if enabled in the library. * **Cons:** * Typically slower execution time compared to native JavaScript's `Set` operations due to the overhead of a library function call. * Higher memory usage since it involves creating and manipulating objects. ### 3. Array Intersection using `.filter()` ```javascript first.filter((f) => second.includes(f)); ``` * **Pros:** * Simple, straightforward implementation leveraging built-in array methods. * Can be easily extended or modified to suit specific use cases. * **Cons:** * Slower execution time and higher memory usage compared to native `Set` intersection due to the need to iterate over both arrays. **Benchmark Results Analysis** The provided benchmark results show that: 1. Native JavaScript's `Set` intersection is the fastest approach, leveraging the efficiency of built-in operations. 2. Lodash's `intersection` function takes a middle ground in terms of execution time and memory usage. 3. Array intersection using `.filter()` yields slower execution times but might be appealing for its simplicity. **Best Approach** The best choice between these methods depends on your specific requirements, such as the need for speed, support across different browsers or environments, and performance characteristics. If you're aiming for the fastest solution, opting for native JavaScript's `Set` intersection could be a good starting point. In conclusion, understanding the pros and cons of each approach helps in selecting the most suitable method for your project's needs, whether that's native JavaScript, Lodash's library functions, or standard array operations.
Related benchmarks:
Lodash Intersection vs. ES6 includes
Lodash Intersection vs. ES6 handle duplicate with set destructuration
custom Set vs Lodash vs custom Array intersection (v3)
Array vs. Set Intersection #2
Lodash vs. Set Intersection on pre-existing sets
Comments
Confirm delete:
Do you really want to delete benchmark?