I would like to check a Javascript Object for unwanted properties with a schema definition for revalidator (https://github.com/flatiron/revalidator).
我想检查一个Javascript对象的不需要的属性与revalidator的模式定义(https://github.com/flatiron/revalidator)。
I came up with following snippet:
我提出了以下片段:
function strip(object, schema) {
var strippedObject = {};
for (var property in schema.properties) {
if (schema.properties[property].type === 'object') {
strippedObject[property] = strip(object[property], schema.properties[property]);
} else {
strippedObject[property] = object[property];
}
}
return strippedObject;
}
This code copies the wanted properties and loops synchronously over the schema recursing into nested schemas.
此代码通过递归到嵌套模式的模式同步复制所需属性和循环。
I'm worried about blocking the event loop in this time.
我担心在这个时候阻止事件循环。
Is this negligible as I'm not doing I/O?
这可以忽略不计,因为我没有做I / O吗?
EDIT
Thanks for the comments. Like jbaylina mentioned it is indeed the case that the schema is nested to a maximum of 2 levels with maybe around 10 properties each. Nevertheless I tried out using setImmediate an it works, but I would maybe iterate asynchronously when it really is a problem:
感谢您的评论。就像jbaylina所提到的那样,确实是架构嵌套到最多2个级别,每个级别可能大约有10个属性。不过我尝试使用setImmediate它可以工作,但我可能会异步迭代它真的是一个问题:
function strip(object, schema, callback) {
var strippedObject = {};
async.each(Object.keys(schema.properties), function (property, next) {
if (schema.properties.hasOwnProperty(property)) {
if (schema.properties[property].type && schema.properties[property].type === 'object') {
strip(object[property], schema.properties[property], function (err, obj) {
if (err) return next(err);
strippedObject[property] = obj;
next();
});
} else {
strippedObject[property] = object[property];
next();
}
}
}, function (err) {
if (err) return callback(err);
return callback(null, strippedObject);
});
}
This looks really messy, but it works and passes the tests. What do you think about this solution?
这看起来非常混乱,但它可以工作并通过测试。您对此解决方案有何看法?
2 个解决方案
#1
1
It is not negligible for large complex object graphs, because it is recursive. Since it is recursive you could easily wrap calling the next recursion inside a setTimeout or setImmediate to free up the event loop.
对于大型复杂对象图,它是不可忽略的,因为它是递归的。由于它是递归的,因此您可以轻松地在setTimeout或setImmediate中包含调用下一个递归以释放事件循环。
Re: Edit this looks good from a performance perspective. It looks like it could be refactored to be more readable but I think you have a solid grasp of the problem domain and the solution you have crafted.
回复:从性能角度来看,编辑看起来不错。看起来它可以被重构为更具可读性,但我认为您已经牢牢掌握了问题域和您制定的解决方案。
#2
0
Unless the schema has thousands of properties, it should not be a problem. Try to measure the time the subrutine takes in the worst case in a "standard envirotment" If that time is not acceptable, you can split this loop in multiple pieces. See Prevent long running javascript from locking up browser
除非架构具有数千个属性,否则它应该不是问题。尝试在“标准环境”中测量最坏情况下的小体切割所需的时间如果该时间不可接受,则可以将该循环分成多个部分。请参阅阻止长时间运行的javascript锁定浏览器
#1
1
It is not negligible for large complex object graphs, because it is recursive. Since it is recursive you could easily wrap calling the next recursion inside a setTimeout or setImmediate to free up the event loop.
对于大型复杂对象图,它是不可忽略的,因为它是递归的。由于它是递归的,因此您可以轻松地在setTimeout或setImmediate中包含调用下一个递归以释放事件循环。
Re: Edit this looks good from a performance perspective. It looks like it could be refactored to be more readable but I think you have a solid grasp of the problem domain and the solution you have crafted.
回复:从性能角度来看,编辑看起来不错。看起来它可以被重构为更具可读性,但我认为您已经牢牢掌握了问题域和您制定的解决方案。
#2
0
Unless the schema has thousands of properties, it should not be a problem. Try to measure the time the subrutine takes in the worst case in a "standard envirotment" If that time is not acceptable, you can split this loop in multiple pieces. See Prevent long running javascript from locking up browser
除非架构具有数千个属性,否则它应该不是问题。尝试在“标准环境”中测量最坏情况下的小体切割所需的时间如果该时间不可接受,则可以将该循环分成多个部分。请参阅阻止长时间运行的javascript锁定浏览器