GeezerVison is showing github.com — Faster subset by dvd101x · Pull Request #3467 · josdejong/mathjs
Conversation
dvd101x
commented
Apr 26, 2025
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Hi,
This improves the speed of subset by following a recommendation in the TODO comments and other recursion improvements.
These are the results

develop
======
subset(matrix, rows and columns) 1.89 µs ±0.13%
subset(array, rows and columns) 4.89 µs ±0.25%
subset(matrix, booleans and columns) 2.15 µs ±0.25%
subset(array, booleans and columns) 5.09 µs ±0.14%
subset(matrix, columns and booleans) 2.15 µs ±0.35%
subset(array, columns and booleans) 5.13 µs ±0.34%
subset(matrix, range and rows) 1.19 µs ±0.20%
subset(array, range and rows) 4.19 µs ±0.16%
subset(matrix, range and all) 1.73 µs ±0.20%
subset(array, range and all) 4.70 µs ±0.16%
subset(matrix, all and range) 2.32 µs ±0.20%
subset(array, all and range) 5.24 µs ±0.19%
subset(matrix, all and all) 4.69 µs ±0.26%
subset(array, all and all) 7.64 µs ±0.20%
this pr
=====
subset(matrix, rows and columns) 1.23 µs ±0.15%
subset(array, rows and columns) 4.17 µs ±0.19%
subset(matrix, booleans and columns) 1.37 µs ±0.18%
subset(array, booleans and columns) 4.30 µs ±0.50%
subset(matrix, columns and booleans) 1.37 µs ±0.18%
subset(array, columns and booleans) 4.29 µs ±0.20%
subset(matrix, range and rows) 0.82 µs ±0.20%
subset(array, range and rows) 3.74 µs ±0.16%
subset(matrix, range and all) 0.81 µs ±0.26%
subset(array, range and all) 3.64 µs ±0.14%
subset(matrix, all and range) 1.10 µs ±0.37%
subset(array, all and range) 3.97 µs ±0.30%
subset(matrix, all and all) 1.64 µs ±0.20%
subset(array, all and all) 4.43 µs ±0.17%
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a neat improvement David! You can't quit improving the performance can't you? 😜
I made a few inline comments, can you have a look at those?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
dvd101x reacted with laugh emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
|
Haha yes, found this while checking something else and couldn't resist.
Thank you for your comments, I will test returning back to matrix.map and matrix.forEach because in the latest commit I didn't see the improvement in performance I expected when making a subset with replacement. Also will add tests for subset with replacement.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@dvd101x so what is the current status? Does the PR still yield good performance improvements after your latest commits, returning to use x.map?
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Hi Jos, thanks for the response. I will just add a big matrix benchmark and I think that will be it.
After your comments and the benchmarks, I think the current implementations are ok. Returning the use of map will benefit readability and would affect performance for big matrices in chrome and in safari a bit less. Firefox would be still ok.
The current logic after many tests I think it's ok to follow this suggestion:
- for stuff that could be big (like matrices or arrays) avoid the use of
array.map and array.forEach, and implement something like a for as done here or on matrix.map and matrix.forEach.
- For small stuff like the size of an array or an index I think it's ok to use
array.map and array.forEach
So I will only add a benchmark for a bigMatrix and all the comments will be addressed.
Thanks for your patience and follow up.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
dvd101x
commented
May 15, 2025
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
Hi, I included a few benchmarks for bigger matrices, and the difference is also significant. More so when the ranges for the subset are further away from 0. In which case the benefits are some of the highest.

With this I think all comments are addressed and it's ready to be merged.
PR 3467
subset(bigMatrix, rows and columns) 1.42 µs ±0.34%
subset(bigArray, rows and columns) 1.40 µs ±0.30%
subset(bigMatrix, range and columns) 1.74 µs ±0.88%
subset(bigArray, rows and range) 1.84 µs ±0.38%
develop
subset(bigMatrix, rows and columns) 1.93 µs ±0.30%
subset(bigArray, rows and columns) 1.95 µs ±1.64%
subset(bigMatrix, range and columns) 3.29 µs ±0.47%
subset(bigArray, rows and range) 4.06 µs ±0.24%
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
👌 Nice, I like how this turned out, this is a significant improvement indeed! Thanks David
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|