vue-bem-cn icon indicating copy to clipboard operation
vue-bem-cn copied to clipboard

Performance

Open pdcmoreira opened this issue 7 years ago • 7 comments
trafficstars

We're binding our classes to a b() method. Doesn't that mean that the b() method gets processed everytime the template re-renders?

What is the performance hit?

pdcmoreira avatar Jun 25 '18 14:06 pdcmoreira

It is just string concatenation. Simple and quick operation with a small number of objects. I don't think that there may be some performance problems.

c01nd01r avatar Jul 02 '18 13:07 c01nd01r

Sure, but even so, calling a bunch of b() every time the template re-renders, might be impactful. It might not scale very well.

pdcmoreira avatar Jul 02 '18 13:07 pdcmoreira

So far we did not have critical performance issues. But I guess there is a small pay-off especially when using b() only with an element string. Which makes sense if you use this plugin. Why should you write classes by yourself. I can imagine that it could make quite a positive difference if :class="b('element')" would just become class="component-element" during build on a greater scale. But I was not able to see how this could be done. I guess you would have to hook into vue-loader to replace this on build. According to the docs it would be possible to remove a directive from the build though: https://github.com/vuejs/vue/tree/dev/packages/vue-template-compiler#options

patric-eberle avatar Sep 30 '18 15:09 patric-eberle

I did test if it would be possible to create a cache based on a stringified configuration object on each call. But looks like JSON.stringify() is slower than the concatenation that is happening inside bemNames().

patric-eberle avatar Oct 08 '18 12:10 patric-eberle

I was looking at this lib and has the same concern. Maybe someone can put a simple demo together and measure the perf?

lmiller1990 avatar Oct 15 '18 15:10 lmiller1990

In the meantime I created a new plugin for us, which is able to reduce the amount of recalculation and also caches hyphenation. See https://www.npmjs.com/package/@verstaerker/vue-bem

patric-eberle avatar Nov 06 '18 10:11 patric-eberle

Unfortunately (for me) I have to admit, that additional test showed that, while my internal code performed faster, the overall rendering time eventually is higher (we are talking about higher two digit milliseconds here). As I currently see it this is due the fact how Vue handles directives. So far I was not able to get it down to the speed that the vue-bem-cn plugin offers...

I actually wanted to also post some testing results, but I was not able to make an extensive comparison yet.

patric-eberle avatar Mar 25 '19 13:03 patric-eberle