Open lelelelemon opened 7 years ago
I'll try to look at this after I have more sleep.
Thanks~ 👍
I'm going to do a bit of changing to this. I think adding this as a helper method might work best.
Also there is a low stock banner & out_of_stock banner. Each needs to be displayed when needed.
Can you show me the benchmarks you saw?
The difference aren't obviously much faster.
My code does some string interpolation Multiple times but that is super fast (plus checks for out of stock vs just low stock which is needed).
Then my code is using rails link_to
vs <a>
tag which should be minimally better.
I have a feeling your big speed improvement is because you aren't checking for the needed out_of_stock product.stock_status
. If you don't need that then removing is fine but I'm leaving it for now.
When rendering the products, link_to, image_tag and some other functions are used to generate corresponding label, however they might be expensive, and the contents generated actually have a lot of things in common. It's possible to use string replace to achieve this function. Also, when the product.status_stock is 'low stock', the same ribbon image will be rendered, so there is no need to generate the image_tag every time, we can put it out of the loop and reuse it when necessary. Here is my patch, it somewhat make the code a little bit hard to read, but it indeed improve performance, especially when one page have a lot of products to show.